相关疑难解决方法(0)

什么是 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

在我的所有UITableView编程中,我总是配置我UITableViewCells的 - [UITableViewDataSource tableView:cellForRowAtIndexPath:].现在我遇到了 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:]方法.这似乎也是进行单元配置的合适场所.

我的问题是:这两种方法之间适当的"分工"是什么?我应该在一对一中做什么?

iphone uitableview

47
推荐指数
1
解决办法
1万
查看次数

滚动时选择UITableViewCell背景颜色更改

我有这些单元格,我已经设置了自定义背景颜色.当我选择单元格时,背景颜色很好,但是,当我向下滚动并向后滚动时,会发生两件事情:

  1. 如果选择的单元格不是很多,则选中时,视图外的单元格有时会返回默认的蓝色.

  2. 如果选择了大部分或全部细胞,那么熄灭的细胞将返回其中预先存在的细胞中的一种颜色 - 即.我选择所有单元格,向下滚动并向上翻转,顶部的单元格与底部的单元格颜色相同(或至少部分单元格 - 其他单元格保留自己的颜色).

这是我生成的代码:

- (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)

iphone scroll objective-c uitableview

3
推荐指数
1
解决办法
2881
查看次数

标签 统计

iphone ×2

uitableview ×2

objective-c ×1

scroll ×1