我有个问题。我实现了一个表格视图。在didSelectRowAtIndexPath
方法中,我增加了单元格的高度并向该单元格的contentView
. 但是,如果我向下滚动表格视图(例如 10 个单元格),我将再次看到相同的文本视图。你能帮我么?
这里有一些代码可以更好地理解问题:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex == indexPath.row)
{
return CELL_HEIGHT * 5;
}
return CELL_HEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
if (selectedIndex == indexPath.row)
{
selectedIndex = -1;
[[cell.contentView viewWithTag:COOL_TAG] removeFromSuperview];
}
else
{
UITableViewCell *previousCell = [aTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0]];
if ([previousCell.contentView viewWithTag:COOL_TAG])
{
[[previousCell.contentView viewWithTag:COOL_TAG] removeFromSuperview];
}
selectedIndex = indexPath.row;
UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(10, 45, 255, 180)];
[text.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
[text.layer setBorderColor:[[UIColor grayColor] CGColor]];
[text.layer setBorderWidth:1.0f];
[text.layer setCornerRadius: 8.0f];
[text.layer setMasksToBounds:YES];
[text setEditable:NO];
text.text = [questionArray objectAtIndex:indexPath.row];
text.tag = COOL_TAG;
[cell.contentView addSubview:text];
}
[tableView beginUpdates];
[tableView endUpdates];
}
提前致谢!