我是 iOS 开发的新手,可以在一个 tableview 中添加两个不同的 nib 单元格,如果可以,你可以显示任何示例。 提前致谢。
最佳答案
你必须注册2个nibs
tableView.registerNib(UINib(nibName: "Cell1", bundle: nil), forCellReuseIdentifier: "Cell1")
tableView.registerNib(UINib(nibName: "Cell2", bundle: nil), forCellReuseIdentifier: "Cell2")
并在数据源中通过 indexPath 选择它们
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellName = (indexPath.row % 2 == 0) ? "Cell1" : "Cell2"
let cell = tableView.dequeueReusableCellWithIdentifier(cellName, forIndexPath: indexPath) as UITableViewCell
return cell
}
what if i wanna to add more then two nib files...
for i in 1...3 {
let cellID = "Cell" + String(i)
tableView.registerNib(UINib(nibName: cellID, bundle: nil), forCellReuseIdentifier: cellID)
}
和
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID = (indexPath.row % 3) + 1
let cellName = "Cell" + String(cellID)
let cell = tableView.dequeueReusableCellWithIdentifier(cellName, forIndexPath: indexPath) as UITableViewCell
return cell
}
关于ios - 如何在一个 Uitableview 中添加两个 nib UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111509/