我试图在iPhone上为UITableView
实现此功能:总是只有FIRST和LAST VISIBLE单元具有不同的背景颜色,例如红色,而其他单元格的颜色保持不变白色。并在滚动过程中进行平滑更改。
我曾尝试:uitableview第一次和最后一次可见的单元格颜色变化
在.m
文件:
NSIndexPath *firstRow;
UITableViewCell* firstCell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"tableCell";
tableCell *cell = (tableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
//cell.thumbnailImageView.image = image;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSArray *visible = [tableView indexPathsForVisibleRows];
firstRow = (NSIndexPath *)[visible objectAtIndex:0];
firstCell = [tableView cellForRowAtIndexPath:firstRow];
firstCell.contentView.backgroundColor=[UIColor redColor];
NSLog(@"main visible cell's row: %i", firstRow.row);
[tableView endUpdates];
firstCell.contentView.backgroundColor=[UIColor redColor];
}
但颜色不会更新滚动备份时。
更多的代码。将代码发布到'cellForRowAtIndexPath' – samfisher 2012-07-26 17:12:44
好吧。 @samfisher – 2012-07-26 17:19:20
是否有你在'willDisplayCell'中调用'cellForRowAtIndexPath'的原因?数据源委托本身调用'cellForRowAtIndexPath';你不应该用其他方法来调用它。 – 2012-07-26 17:41:26