我拼命地尝试实现以下效果:我有一个表格 View ,我想在其中展开选择的单元格并显示一些附加信息。我重复使用表格单元格,仅供引用。过去我能够在另一个应用程序中实现这样的效果,但这对我来说似乎根本不起作用。所有对于发生这种情况至关重要的方法都被调用并且行展开得很漂亮,但是 showExpandedContents
方法中的 additionalInfo
表不会出现!
- 如果我在单元格初始化时创建
additionalInfo
表,该表将会出现,但是当我然后尝试加载数据时(由此,还有单元格)它只是不会重新加载表格。 但是负责此 (showExpandedContents
) 表的方法肯定会被调用。 - 如果我初始化 AND 在单元格的初始化代码中设置表格,一切正常。
所以这里有一些代码给你:
这里是单元设置:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
((MyCustomCell*)cell).data = [arrayToDisplay objectAtIndex:indexPath.row];
return cell;
}
这里是选择方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//setting the current cell as the expandedRow, so that I can collapse it later
expandedRow = [tableView cellForRowAtIndexPath:indexPath];
[((MyCustomCell*)expandedRow) showExpandedContents];
[tableView beginUpdates];
[self changeRowHeightAtIndex:indexPath.row height:330];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
这是 MyCustomCell
中的 -showExpandedContents
方法,我想在其中显示附加信息:
-(void)showExpandedContents{
additionalInfo = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.frame.size.width, 250)];
[additionalInfo loadContent];
additionalInfo.backgroundColor = UIColorFromRGB(0xf9f9f9, 1.0);
additionalInfo.layer.zPosition = MAXFLOAT;
[self.contentView addSubview:additionalInfo];
}
最佳答案
看起来像你的
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
为您提供了一个新的单元格,与调用了 showExpandedContents
的单元格不同。
删除此行应该可以解决问题。
关于ios - 将 UIView 添加到重用的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19735893/