作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我生成了一个像 Accordion 一样的 UITableView,例如:
关闭:
打开:
我目前有以下代码,当前为项目 1 和子项目设置样式。
问题是我无法更新单元格的子项字体/文本颜色。我可以改变背景颜色。
这真是令人困惑。
你能提供一个解决方案吗?
这是表格 View 当前状态的屏幕截图:
这里是一些相关的代码:
- (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/
我生成了一个像 Accordion 一样的 UITableView,例如: 关闭: 项目 1 打开: 项目 1 子项1 我目前有以下代码,当前为项目 1 和子项目设置样式。 问题是我无法更新单元格的子
我有一个 PostListTableCell 类,它继承了 UITableViewCell,用作我在 UITableView 中的自定义单元格。 我需要调整单元格中一些标签的大小,所以我在中调用了以下
我是一名优秀的程序员,十分优秀!