UITableView à l'aide de Swift

Mon TapCell1.swift

Ceci est Custom UITableViewCell View

 import UIKit class TapCell1: UITableViewCell { @IBOutlet var labelText : UILabel init(style: UITableViewCellStyle, reuseIdentifier: Ssortingng!) { println("Ente") super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier) } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } } 

Mon ViewController.swift

Son All DataSource et Delegates sont définis correctement. Mais ma cellule personnalisée ne s'affiche pas.

 import UIKit class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var label: UILabel @IBOutlet var tableView : UITableView var getvalue = NSSsortingng() override func viewDidLoad() { super.viewDidLoad() label.text="HELLO GOLD" println("hello : \(getvalue)") self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell") } func tableView(tableView:UITableView!, numberOfRowsInSection section:Int)->Int { return 5 } func numberOfSectionsInTableView(tableView:UITableView!)->Int { return 1 } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1 cell.labelText.text="Cell Text" return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Le problème est Ma cellule personnalisée n'est pas affichée. S'il vous plaît suggérer quelque chose que j'ai mal fait.

Note: voici Mon code Mon lien de téléchargement de file

Je l'ai finalement fait.

Pour TapCell1.swift

 import UIKit class TapCell1: UITableViewCell { @IBOutlet var labelTitle: UILabel init(style: UITableViewCellStyle, reuseIdentifier: Ssortingng!) { super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier) } } 

Pour NextViewController.swift

 import UIKit class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITableView var ListArray=NSMutableArray() override func viewDidLoad() { super.viewDidLoad() let nibName = UINib(nibName: "TapCell1", bundle:nil) self.tableView.registerNib(nibName, forCellReuseIdentifier: "Cell") for i in 0...70 { ListArray .addObject("Content: \(i)") } } func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)->Int { return ListArray.count } func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 51 } func numberOfSectionsInTableView(tableView: UITableView!) -> Int { return 1 } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1 //cell.titleLabel.text = "\(ListArray.objectAtIndex(indexPath.item))" cell.labelTitle.text = "\(ListArray.objectAtIndex(indexPath.row))" return cell } } 

Mon lien de code de travail: TABLE PERSONNALISÉE

Vous devriez save la class pour la cellule. Pour cela, changez cette ligne de code

 self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell") 

modifier

Vous code est très bien je l'ai vérifié

 //cell.labelText.text="Cell Text" cell.textLabel.text="Cell Text" // use like this 

La solution est très probablement définie pour définir manuellement la hauteur de la cellule en tant que telle:

 override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat { return 44 } 

Je crois que c'est un bogue Xcode beta 6.

Vérifiez votre tableau d'histoire select la cellule et regardez l'inspecteur d'identité dans cette sélection CLASS tapez votre CustomClass et MODULE tapez votre nom de projet

Je l'ai fait Cela fonctionne parfaitement essayer cette astuce pour éviter l'erreur de voir ci-dessous l'image et le code

 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { // let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil) // cell.textLabel.text = array[indexPath!.row] as Ssortingng let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell cell.nameLabel.text = array[indexPath!.row] as Ssortingng cell.RestaurentView.image = UIImage(named:images[indexPath!.row]) return cell } 

J'ai maintenant réussi à faire fonctionner Custom UITableViewCell.

Travaille sur

  • Fonctionne sur Xcode 6 beta 6
  • Fonctionne sur Xcode 6 beta 5

  • iOS est 7.1

Comment

  • J'ai créé un file ".xib" pour la cellule.
  • Je le copy dans le storyboard.
  • Via le storyboard, je lui donne un identifiant.
  • Je m'assure que c'est un sous-enfant d'une tableview

De cette façon, vous n'avez pas besoin d'save une class / nib etc.

C'est ma cellule personnalisée.

 importer UIKit

 class TestCell: UITableViewCell {

     @IBOutlet var titleImageView: UIImageView!
     @IBOutlet var titleLabel: UILabel!

     override init (style: UITableViewCellStyle, reuseIdentifier: Ssortingng!) {
         super.init (style: style, reuseIdentifier: reuseIdentifier)
     }

     override func awakeFromNib () {
         super.awakeFromNib ()
         // Code d'initialisation
     }

     init requirejs (codeur aDecoder: NSCoder) {
         super.init (codeur: aDecoder)
     }
 }

À votre avis, ou partout où vous étendez "UITableViewDataSource". Assurez-vous que "cell2" est le même que le "Identifier" que vous lui avez donné via le storyboard.

 func tableView (tableView: UITableView !, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {

     var cell: TestCell = tableView.dequeueReusableCellWithIdentifier ("cell2", forIndexPath: indexPath) en tant que TestCell

     // Exemple d'utilisation des éléments personnalisés.
     cell.titleLabel.text = self.items [indexPath.row]

     var topImage = UIImage (nommé: "fv.png")
     cell.titleImageView.image = topImage

     returnner la cellule
 }

uitableviewcell

Essayez ce code suivant

var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell

https://github.com/iappvk/TableView-Swift

Si vous n'utilisez pas Storyboard, créez un nouveau file en tant que sous-class de UITableViewCell. Cochez la case "créer aussi des files xib" après avoir défini votre cellule personnalisée .et voici le code pour tableview

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var customcell:CustomTableViewCellClass? = tableView.dequeueReusableCellWithIdentifier("cell") as? CustomTableViewCellClass if (customcell==nil) { var nib:NSArray=NSBundle.mainBundle().loadNibNamed("CustomTableViewCellClass", owner: self, options: nil) customcell = nib.objectAtIndex(0) as? CustomTableViewCell } return customcell! } 

C'est une notation purement rapide qui fonctionne pour moi

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cellIdentifier:Ssortingng = "CustomFields" var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell if (cell == nil) { var nib:Array = NSBundle.mainBundle().loadNibNamed("CustomCell", owner: self, options: nil) cell = nib[0] as? CustomCell } return cell! }