我試圖在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