【发布时间】:2017-03-29 14:20:33
【问题描述】:
我有一个项目(由其他人编写),其中有一个在表格视图中显示内容和文本的提要。每篇文章对应表格视图中的一个部分,每个部分都有自己的行,对应于内容、文本、按钮等元素。
我需要在表格视图单元格内显示带有“更多”按钮的帖子标题的短标签,当点击更多按钮时,标签将扩展到标题适合的任何大小,所有这些都发生在表格视图单元格内。当点击更多按钮时,我将标签的numberOfLines 属性更改为零,并且由于单元格具有自动高度,我只需要重新加载该特定标题单元格。 (如果我在显示单元格之前首先将 numberOfLines 设置为 0,则单元格会以展开后的大小正确显示。)
我试过了:
[tableView beginUpdates];
tableView endUpdates];
我尝试了各种动画选项:
[tableView reloadRowsAtIndexPaths:@[myPath] withRowAnimation:UITableViewRowAnimation(Bottom,Top,None etc)];
我试过了:
[tableView reloadSections:[NSIndexSet indexSetWithIndex:myPath.section] withRowAnimation:UITableViewRowAnimation(Top,Bottom,None etc)];
但它们都产生相同的结果:整个表格视图布局变得混乱:它跳转到另一个单元格,一些视图变为空白,单元格相互重叠,单元格内的视频停止播放,并且标签没有展开(但会在内部刷新,例如,带有一行的简短预览 txt 会从顶部/底部等动画,但不会展开)。
什么可能导致整个表格视图混乱,以及如何正确地重新加载一个单元格和展开动画,而不会弄乱整个布局?我已经看到了很多关于此的问题和答案,但他们都推荐了我已经尝试过并在上面解释过的选项。
我的应用面向 iOS 8.0+
更新:以下是相关代码(删除了一些与布局无关的内部工作部分):
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier: MyCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.indexPathToReloadOnAnimation = indexPath;
cell.shouldShortenCaption = YES;
id post = self.posts[indexPath.section] ;
[cell setPost:post];
return cell;
在setPost:里面:
if(self.shouldShortenCaption){
captionLabel.numberOfLines = 2;
}else{
captionLabel.numberOfLines = 0;
}
NSString *text = [some calculated (deterministic) text from post object];
按钮操作很简单:
self.shouldShortenCaption = NO;
[one of the reload codes that I've written above in the question]
更新 2:以下是有关此问题的更多方法:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section < self.posts.count) {
return 59;
}else
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section < self.posts.count) {
MyFeedHeader *header = [tableView dequeueReusableCellWithIdentifier:MyFeedHeaderIdentifier];
header.delegate = self;
[header setPostIndex:section];
[header setPost:self.posts[section]] ;
return header;
}
else
return nil;
}
Header 的setPost: 方法基本上是将相关文本设置为标签(与标题无关,它们是完全不同的单元格。有问题的单元格不是标题单元格)。该表没有任何页脚方法。关于高度的唯一方法是上面的方法。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section >= self.posts.count) {
return 1;
}
id post = self.posts[section];
[calculate number of rows, which is deterministic]
return [number of rows];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
BOOL hasCursor = self.cursor && self.hasMore ? 1 : 0;
return self.posts.count + hasCursor;
}
(post count 和 cursor 和 hasMore 也是确定性的)。
更新 3: 我问了一个问题(虽然有类似的问题,但不是重复的)并得到了一个有用的答案来解决我的问题。投反对票的人能否详细说明他们投反对票的原因?
【问题讨论】:
-
我们能看到 cellForRow... 和按钮操作代码吗?
-
另外,
UITableViewDataSource和UITableViewDelegat中的任何方法都可能会有所帮助。 -
@danh 我已经添加了
-
@Losiowaty 我已经添加了 cellForRow 代码。对于代表,您到底想要什么方法?代码非常复杂,单个文件中有 1000 多行(正如我所说,这不是我编写的代码),在此处发布整个文件既不切实际又非法。如果您问我需要哪一种,我可以添加相关方法。
-
好吧,我在考虑
viewForHeader/FooterInSection:,如果你有的话。由于您有自动高度,因此很可能没有heightForRowAtIndexPath;) 也许numberOfSection和numberOfRows,如果那里发生了一些不平凡的事情。
标签: ios objective-c uitableview uitableviewautomaticdimension