【发布时间】:2018-07-27 11:06:55
【问题描述】:
我是 Swift(和编码)的新手,所以请多多包涵。在我的桌子上,我设置了单元格,这样当我向右滑动时,我会看到两个选项:“完成”和“删除”。当我单击“删除”时,单元格行被删除。当我点击“完成”时,我希望单元格行改变颜色。我不知道我是怎么走到这一步的,但是当我点击“完成”时,整个表格改变了单元格行的颜色except。我尝试将我的可重用标识符用于单元格原型,我将其设置为“单元格”,但这没有成功,因为我不确定如何声明“单元格”而不干扰我的 CellForRowAt 函数。
这是我的代码:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
self.habits.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print(self.habits)
}
let done = UITableViewRowAction(style: .default, title: "Done") { (action, indexPath) in
// save item at indexPath
tableView.backgroundColor = UIColor.lightGray
print("Done")
}
done.backgroundColor = UIColor.green
return [delete, done]
}
由于前面的代码块,我认为我不能声明“单元格”:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue Resuable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
let Habit = habits[indexPath.row]
cell.textLabel?.text = Habit.mainGoal
cell.detailTextLabel?.text = Habit.microGoal
return cell
再次,我是编码新手。我已经尝试查找这个问题,但不太确定我的措辞是否正确,但我找不到答案。我的备用想法是删除单元格中的标签,但从我收集的内容来看,这更复杂。
【问题讨论】:
标签: swift uitableview row cell