【发布时间】:2016-02-22 11:09:58
【问题描述】:
我在刷新 UIViewController 中的自定义 UITableView 时遇到问题。
当 tableView 出现时,它的所有单元格都具有清晰的背景色。 我在上面有一个“开始”按钮,当我点击它时,我想让所有单元格都变成另一种颜色。
我已经在:
中指定了规则 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if self.status == "start" {
if indexPath != self.currentIndexPath {
cell.backgroundColor = UIColor(red: 0 , green: 0, blue: 0, alpha: 0.5)
} else {
cell.backgroundColor = UIColor.clearColor()
}
} else {
cell.backgroundColor = UIColor.clearColor()
}
}
在“开始”操作按钮中,我调用:self.tableView.reloadData
@IBAction func startAction(sender: AnyObject) {
self.currentIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.status = "start"
self.tableView.reloadData()
}
但效果不是很好,因为我必须滚动才能更新背景颜色。
我尝试使用self.tableView.reloadRowsAtIndexPaths 方法。但结果是一样的。
我总是必须滚动 tableView 来更新背景颜色或一些图像。
我哪里错了?
【问题讨论】:
-
你有 self.currentIndexPath 吗?因为我在 func startAction(sender: AnyObject) 中看不到任何与 self.currentIndexPath 相关的代码
-
我只是忘记在这里引用它,但它在我的代码中定义得很好。我用 currentIndexPath 更新了上面的代码
-
请检查您是否分配了tableView IBOutlet。
-
@jay:当然是的。
标签: ios xcode swift uitableview