gpt4 book ai didi

ios - 是否可以为每个部分设置单元格背景颜色?

转载 作者:行者123 更新时间:2023-11-28 22:17:07 27 4
gpt4 key购买 nike

我正在尝试根据节号设置单元格背景颜色,但效果不佳。当我来回滚动时,单元格颜色发生变化并且是错误的。我正在尝试使用在 willDisplayHeaderView 中设置然后在 willDisplayCell 中使用的颜色变量来完成此操作。这是代码:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view     forSection:(NSInteger)section{

if(section % 2 == 0){
self.currentCellColor =[UIColor colorWithRed:(199/255.0) green:(214/255.0) blue:(156/255.0) alpha:1];
}
else{
self.currentCellColor = [UIColor colorWithRed:(242/255.0) green:(245/255.0) blue:(232/255.0) alpha:1];
}
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = self.currentCellColor;
}

此外,部分对于这项功能并不重要,因此如果有人有更好/更简单的方法在一个大部分中实现这一点,请随时提出建议。我只需要每 4 行更改一次颜色。

最佳答案

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
....

if (indexPath.section % 2) {
cell.contentView.backgroundColor = [UIColor redColor];
}
else {
cell.contentView.backgroundColor = [UIColor blueColor];
}

return cell;
}

关于ios - 是否可以为每个部分设置单元格背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396410/

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