gpt4 book ai didi

ios - 更改 UIViewTableCell 样式不起作用

转载 作者:行者123 更新时间:2023-11-28 21:46:51 25 4
gpt4 key购买 nike

我生成了一个像 Accordion 一样的 UITableView,例如:

关闭:

  • 项目 1

打开:

  • 项目 1
    • 子项1

我目前有以下代码,当前为项目 1 和子项目设置样式。

问题是我无法更新单元格的子项字体/文本颜色。我可以改变背景颜色。

这真是令人困惑。

你能提供一个解决方案吗?

这是表格 View 当前状态的屏幕截图:

Current table view... you can see that the sub items font/color have not changed

这里是一些相关的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ParentCellIdentifier = @"ParentCell";
static NSString *ChildCellIdentifier = @"ChildCell";

BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

UITableViewCell *cell;

if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
}


if (isChild) {
cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
}
else {
int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
cell.textLabel.text = [topItems objectAtIndex:topIndex];
cell.detailTextLabel.text = @"";
}

NSUInteger row = indexPath.row;
if(!isChild){
switch (row) {
case 0:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.529 green:0.655 blue:0.733 alpha:1];
break;
case 1:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.176 green:0.282 blue:0.341 alpha:1];
break;
case 2:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.51 green:0.357 blue:0.42 alpha:1];
break;
case 3:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.808 green:0.561 blue:0.216 alpha:1];
break;
case 4:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.631 green:0.655 blue:0.282 alpha:1];
break;
case 5:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.753 green:0.314 blue:0.235 alpha:1];
break;
case 6:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.431 green:0.176 blue:0.416 alpha:1];
break;
case 7:
cell.contentView.backgroundColor = [UIColor colorWithRed:0.078 green:0.506 blue:0.506 alpha:1];
break;
default:
break;
}

cell.textLabel.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[cell textLabel] setFont:[UIFont fontWithName:@"LoveYaLikeASisterSolid" size:14.0]];
cell.textLabel.textColor = [UIColor whiteColor];
}
else{
NSLog(@"Row %lu",(unsigned long)row);
}

return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

if (isChild) {
NSLog(@"A child was tapped, do what you will with it");
return;
}

[self.tableView beginUpdates];

if (currentExpandedIndex == indexPath.row) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
else {

BOOL shouldCollapse = currentExpandedIndex > -1;

if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}

currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

[self expandItemAtIndex:currentExpandedIndex];
}

[self.tableView endUpdates];

}

- (void)expandItemAtIndex:(int)index {
NSLog(@"expading");
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [subItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

[indexPaths release];
}

- (void)collapseSubItemsAtIndex:(int)index {
NSLog(@"collapsing");
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[indexPaths release];
}

谢谢

最佳答案

正在重复使用该单元格。您需要为该样式创建一个新的单元格实例(然后您可以重复使用)。为不同的样式创建单独的单元格并返回每个。

if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ChildCellIdentifier];
}
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ParentCellIdentifier];
}
}

关于ios - 更改 UIViewTableCell 样式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828840/

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