-1

我是 Objective C 的新手,我对正在处理的表非常迷茫。我想要的是,当我单击单元格时,它会展开并向我显示一个子视图,它是另一个标签,然后当我再次单击它时,它会隐藏子视图并显示原始文本。目前这一切都发生了可怕的错误,我真的不知道我应该如何编码。前提是我有一个问题可以扩展以揭示答案,我认为这很容易......这是我的代码:

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];

// create the subview and apply it to the current cell selected
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = @"OH";
[cell.contentView addSubview:label];

// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];

// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];

// update the view that holds the table
[firstView beginUpdates];
[firstView endUpdates];

//Change cell contents
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

}

如果有人可以提供一些帮助,甚至更好地更改代码以向我展示我应该如何去做,那将是非常棒的,因为我不知所措,需要在接下来的几个小时内完成这项工作!哎呀。

谢谢!

4

1 回答 1

0

首先,每次调用此方法时,您都会将标签添加到 contentView ,无论是选择还是取消选择单元格。只有当单元格原本未被选中时才需要添加标签,然后再次触摸时需要将其移除。您需要保留对标签的引用(或在其上设置标签),以便您可以调用removeFromSuperview

[label removeFromSuperview];

我也不清楚如果单元格滚动到屏幕外并重新使用(在被选中时)会发生什么;标签是否添加回单元格中cellForRowAtIndexPath?如果是这样,为什么还要在这里添加它?

其次,请添加您的cellForRowAtIndexPath方法,因为这可能是您将文本设置为数组值的地方(这样我们就可以尝试解决您的实际问题)。

于 2012-08-23T19:33:09.430 回答