用 tableview(_:cellForRowAtIndexPath) 方法在 tableView 的某 cell 中 addSubView 之后,在其他个别 cell 里也出现了这个被添加的 view ,虽然通过判断这个 view 是不是存在来删除它,但好奇为什么会出现这种现象。
本来是猜测 cell 在加载前并不会初始化, view 如果没有通过 indexPath 来修改,那么后面出现的 cell 都会受前面 cell 的影响。但仔细想如果是这样,那为什么不是后来每个 cell 里都会有这个 view 呢?
代码大致是
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomViewCell
let viewToAdd = UILable(frame: cell.contentView.frame)
...
if (condition) {
...
} else {
...
cell.contentView.addSubview(viewToAdd)
}
return cell
}
所以好奇这个方法的实现逻辑到底是怎么样的……