UITableView使用Swift

我的TapCell1.swift

这是自定义的UITableViewCell View

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

我的ViewController.swift

它的所有数据源和委托都设置正确。但我的自定义单元格不显示。

 import UIKit class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var label: UILabel @IBOutlet var tableView : UITableView var getvalue = NSString() 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. } } 

问题是我的自定义单元格不显示。 请build议我做错了什么。

注意:这里是我的代码我的文件下载链接

我终于做到了。

对于TapCell1.swift

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

对于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 } } 

我的工作代码链接: CUSTOMIZED TABLE

你应该注册该单元的class 。 为此,请将此行代码更改为

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

编辑

你的代码看起来很好,我检查了它

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

该解决scheme很可能是人为地设置细胞高度的方式:

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

我相信这是一个Xcodetesting版6的错误。

检查你的故事板select单元格,并看看“身份检查员在该selectCLASS键入您的CustomClass和MODULE键入您的项目名称

我已经完成了它完美的作品尝试这个提示,以避免错误,看到下面的图像和代码

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

我现在已经能够得到自定义UITableViewCell工作。

在工作

  • 运行在Xcode 6 beta 6上
  • 在Xcode 6 beta 5上运行

  • iOS是7.1

怎么样

  • 我为单元格创build了一个“.xib”文件。
  • 我把它复制到故事板。
  • 通过故事板,我给它一个标识符。
  • 我确定它是一个tableview的子小孩

这样做,你不需要注册一个类/笔尖等。

这是我的自定义单元格。

导入UIKit

类TestCell:UITableViewCell {

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

    重写init(样式:UITableViewCellStyle,reuseIdentifier:String!){
         super.init(style:style,reuseIdentifier:reuseIdentifier)
     }

    覆盖func awakeFromNib(){
         super.awakeFromNib()
         //初始化代码
     }

    需要的init(编码器aDecoder:NSCoder){
         super.init(编码器:aDecoder)
     }
 }

在你看来,或者你在哪里扩展“UITableViewDataSource”。 确保“cell2”与您通过故事板给出的“标识符”相同。

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

     var cell:TestCell = tableView.dequeueReusableCellWithIdentifier(“cell2”,forIndexPath:indexPath)as TestCell

     //使用自定义元素的示例。
     cell.titleLabel.text = self.items [indexPath.row]

     var topImage = UIImage(名称:“fv.png”)
     cell.titleImageView.image = topImage

    返回单元格
 }

的UITableViewCell

试试下面的代码

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

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

如果你没有使用Storyboard,那么创build一个新的文件作为UITableViewCell的子类,勾选“也创buildxib文件”checkbox后,设置你的自定义单元格。这里是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! } 

纯粹的快速符号为我工作

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cellIdentifier:String = "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! }