【问题标题】:how to mentain cell colour of tableview after scrolling in iOS在iOS中滚动后如何保持tableview的单元格颜色
【发布时间】:2013-05-06 07:00:59
【问题描述】:

我正在更改自定义 tableView 按钮颜色上的单元格颜色。单元格 indexRow 在按钮单击时变为绿色,但在滚动后再次变为正常的白色。我尝试了不同的解决方案,但没有结果。你能建议一下吗?谢谢。

-(void)yesAction:(id)sender
{
 _cellButtonPress = YES;
// [myChklist reloadData];
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
UITableView *curTableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [curTableView indexPathForCell:cell];
cell.backgroundColor = [UIColor greenColor];
}

【问题讨论】:

    标签: ios uitableview colors


    【解决方案1】:

    当您的单元格滚动时,它会再次被绘制。需要重新设置属性。

    您可以维护一个选定索引的数组。然后在您的 cellForRowAtIndexPath 委托方法中,您可以根据索引将背景颜色设置为白色或绿色。类似于:

      NSMutableArray* indexArray = [[NSMutableArray alloc] init];
    

    在 yesAction 中:

     [indexArray addObject:indexPath];
    

    在 cellForRowAtIndexPath 中:

     if ([indexArray containsObject:indexPath]) {
        cell.backgroundColor = [UIColor greenColor];
    }
    

    【讨论】:

    • 是的,gr8。我也把这个方法放在 - (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多