gpt4 book ai didi

ios - uitableview第一个和最后一个可见单元格颜色变化

转载 作者:行者123 更新时间:2023-12-01 17:59:54 25 4
gpt4 key购买 nike

我试图在iPhone的UITableView中实现此功能:始终只有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];
}

但是向上滚动时颜色不会更新。

最佳答案

如果需要,您可以在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;
}

关于ios - uitableview第一个和最后一个可见单元格颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com