0
我產生我有演技像如手風琴一個UITableView:更改UIViewTableCell風格不工作
關閉:
- 項目1
開:
- item 1
- 子物品1
目前,我有下面的代碼,目前風格無論是第1項和子項目。
問題是我無法更新單元格的子項目字體/文本顏色。我可以改變背景顏色。
這真的很讓人困惑。
您能否提供解決方案?
下面是表視圖的當前狀態的屏幕截圖:
下面是一些有關的代碼的:
- (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];
}
由於
該單元格正在被重用。如果你想創建一個不同樣式的單元格,那麼不要使用deque(重用)。 –
@DCGoD你能告訴我在哪裏調整該片段嗎? ta – cwiggo
除了'cell.textLabel'之外,是否應該修改'cell.detailTextLabel'的屬性? –