我知道您不能在单个 UITableView
中混合静态和动态单元格类型,但我想不出更好的方式来描述我的问题。
我有几个具有固定内容的预定单元格,我还有未知数量的具有动态内容的单元格位于中间。所以我想让我的 table 看起来像这样:
Fixed
Fixed
Fixed
Dynamic
Dynamic
Dynamic
Dynamic
Dynamic
Fixed
Fixed
那么你究竟建议我如何在我的 cellForRowAtIndexPath
方法中处理这个问题?
谢谢。
最佳答案
苦苦挣扎1天,找到最佳解决方案
为 Swift 4 更新
对于问题的情况:
在要应用动态单元格的第二部分添加一个空的tableview单元格,将单元格的标识符更改为WiFiTableViewCell
创建一个名为 WiFiTableViewCell
在tableViewController的ViewDidLoad函数中注册Nib
tableView.register(UINib(nibName: "WiFiTableViewCell", bundle: nil), forCellReuseIdentifier: "WiFiTableViewCell")
添加以下代码以同时使用动态和静态单元格
override func numberOfSections(in tableView: UITableView) -> Int
{
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return self.dataSource.count
//the datasource of the dynamic section
}
return super.tableView(tableView, numberOfRowsInSection: section)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "WiFiTableViewCell") as! WiFiTableViewCell
return cell
}
return super.tableView(tableView, cellForRowAt: indexPath)
}
override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: NSIndexPath) -> Int {
if indexPath.section == 1 {
let newIndexPath = IndexPath(row: 0, section: indexPath.section)
return super.tableView(tableView, indentationLevelForRowAt: newIndexPath)
}
return super.tableView(tableView, indentationLevelForRowAt: indexPath)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 {
return 44
}
return super.tableView(tableView, heightForRowAt: indexPath)
}
https://stackoverflow.com/questions/18153702/