gpt4 book ai didi

ios - 仅针对1个tableViewCell项目的颜色更改每10个项目重复一次

转载 作者:行者123 更新时间:2023-12-01 16:04:38 25 4
gpt4 key购买 nike

我希望tableView的第一项与列表的其余部分具有不同的颜色。

所以我:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{

static NSString *CellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.textLabel.text = [_content objectAtIndex:indexPath.row];

if (indexPath.row==0){
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.6 alpha:1];
}
return cell;
}
  • 发生了奇怪的现象。在列表的后面10个项目中,该项目也更改了颜色。以为可能是“0”重复发生。所以我尝试了:

    如果([cell.textLabel.text isEqualToString:@“我的第一项的标题”]] {
    cell.textLabel.textColor = [UIColor colorWithRed:0绿色:0蓝色:0.6 alpha:1];
  • 很好,以后有10个项目,仍在更改该项目的颜色。有任何想法吗?
  • 最佳答案

    UITableView不会为每一行分配一个新的单元格。而是,为了节省内存,您调用dequeueReusableCellWithIdentifier :,它获取以前已分配的单元格。我怀疑发生的事情是您的屏幕可以容纳10个单元格,因此当您的第一个单元格滚动出屏幕时,它会被单元格11重用,但其文本颜色保持不变。要解决此问题,只需在您的颜色分配中添加else语句。

    if (indexPath.row==0){
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.6 alpha:1];
    }
    else {
    cell.textLabel.textColor = [UIColor someOtherColor];
    }

    这样,当第一个单元格被重用时,其颜色将被重置。

    关于ios - 仅针对1个tableViewCell项目的颜色更改每10个项目重复一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20587398/

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