【发布时间】:2016-12-21 10:04:19
【问题描述】:
我有一个 TableViewController,它有 3 个部分和它们自己的标题。 现在我想在插入任何单元格之前,检查一个属性,然后将单元格添加到不同的部分。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "TasksTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell else {
fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
}
// Fetches the appropriate task for the data source layout.
let task = tasks[indexPath.row]
cell.nameLabel.text = task.name
cell.photoImageView.image = task.photo
cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")
if(task.importanceLevel == 0){
// add cell to section 0
}
else if(task.importanceLevel == 1){
// add cell to section 1
}
// Configure the cell...
return cell
}
你能看到评论吗,有什么办法吗? 非常感谢
【问题讨论】:
-
我认为你应该改变你的数据模型。您需要通过“importanceLevel”参数为每个任务确定部分中的行数。 'cellForRowAt' 方法应该只为部分中的每一行显示正确的单元格。过滤数据必须在此之前完成
-
blog.apoorvmote.com/… 这是基本的请通过...
-
有这方面的样本吗?谢谢
-
仍然不是我想要的 :((。但我确实改变了 sth: section = ['a', 'b', 'c'] 并成功显示这些部分。我想这样做:当检查 if (task.importanceLevel = 0) 然后将单元格添加到“a”部分,依此类推
标签: swift uitableview