swift总结
一、注册cell
1.用nib, 向 tableview 注册 cell
全局变量 let cellIdentifier = "myCell"
myTableView!.registerNib(UINib(nibName: "MyCell", bundle:nil), forCellReuseIdentifier: cellIdentifier)
然后在 cellForRowAtIndexPath 方法中使用:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
var mycell : MyCell = tableView!.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyCell
return mycell
}
2.用纯代码,向tableView注册cell
全局变量 let cellIdentifier = "myCell"
self.tableView.registerClass(MyCell.classForKeyedArchiver(), forCellReuseIdentifier: cellIdentifier)
然后在 cellForRowAtIndexPath 方法中使用
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyCell
return cell
}
3.不注册cell,系统直接创建
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let identifier = "CellIdentifier"
var cell :UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
cell = MyCell(style: .Default, reuseIdentifier: identifier)
var string :String = typeArray[indexPath.row] as String
cell!.textLabel.text = string
return cell
}
二、UIViewController 无法服从 UITableViewDataSource 所以最好用UITableViewController