我正试图在UITableViewiPhone中实现这个功能:总是只有FIRST和LAST VISIBLE单元格有不同的背景颜色,比如红色,而其他单元格的颜色保持白色.滚动期间的平滑变化.
我试过了:
在.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];
}
Run Code Online (Sandbox Code Playgroud)
但滚动回来时颜色不会更新.
小智 6
如果需要,可以在cellForRowAtIndexPath中完成所有操作.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tableViewCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellID];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellID];
[[cell textLabel] setText:[NSString stringWithFormat:@"some sting %i", [indexPath row]]];
NSArray *visibleCells = [tableView visibleCells];
for (int i = 0; i < [visibleCells count]; i++) {
UITableViewCell *cell = [visibleCells objectAtIndex:i];
if (i == 0 || i == [visibleCells count] - 1)
[cell setBackgroundColor:[UIColor redColor]];
else
[cell setBackgroundColor:[UIColor clearColor]];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
4563 次 |
| 最近记录: |