在我的所有UITableView编程中,我总是配置我UITableViewCells的 - [UITableViewDataSource tableView:cellForRowAtIndexPath:].现在我遇到了 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:]方法.这似乎也是进行单元配置的合适场所.
我的问题是:这两种方法之间适当的"分工"是什么?我应该在一对一中做什么?
我有这些单元格,我已经设置了自定义背景颜色.当我选择单元格时,背景颜色很好,但是,当我向下滚动并向后滚动时,会发生两件事情:
如果选择的单元格不是很多,则选中时,视图外的单元格有时会返回默认的蓝色.
如果选择了大部分或全部细胞,那么熄灭的细胞将返回其中预先存在的细胞中的一种颜色 - 即.我选择所有单元格,向下滚动并向上翻转,顶部的单元格与底部的单元格颜色相同(或至少部分单元格 - 其他单元格保留自己的颜色).
这是我生成的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *row = [tableView cellForRowAtIndexPath:indexPath];
UIView *backview = [[UIView alloc] initWithFrame:row.frame];
backview.backgroundColor = [colours objectAtIndex:indexPath.row];
row.selectedBackgroundView = backview;
}
Run Code Online (Sandbox Code Playgroud)
这就是所选择的细胞方法改变颜色的地方.单元格在这里创建:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"eventTypeID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *sEventType = [[self.eventTypes valueForKeyPath:@"name.text"] objectAtIndex:indexPath.row];
cell.textLabel.text = sEventType;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
每个单元格的颜色都在这里设置:
- (void)loadView {
colours = …Run Code Online (Sandbox Code Playgroud)