这是我想要在我的应用程序中使用的内容。下面显示的是 iPhone 应用商店的两个屏幕截图:
我基本上需要一个“阅读更多”功能,就像在应用程序商店中使用的一样(参见上面两张图片中的“描述”部分)。我假设此处的每个部分(描述、新增内容、信息 等)都是一个表格 View 单元格。里面的文本是一个 UILabel 或 UITextField。
这是我到目前为止尝试添加此功能的方法:
NSString *initialText = @"Something which is not a complete text and created just as an example to...";
NSString *finalText = @"Something which is not a complete text and created just as an example to illustrate my problem here with tableviews and cel heights. bla bla bla";
NSInteger isReadMoreTapped = 0;
我的 cellForRowAtIndexPath 函数:
// Other cell initialisations
if(isReadMoreTapped==0)
cell.label.text = initialText;
else
cell.label.text = finalText;
return cell;
我的 heightForRowAtIndexPath 函数:
// Other cell heights determined dynamically
if(isReadMoreTapped==0){
cell.label.text = initialText;
cellHeight = //Some cell height x which is determined dynamically based on the font, width etc. of the label text
}
else{
cell.label.text = finalText;
cellHeight = //Some height greater than x determined dynamically
}
return cellHeight;
最后是我的 IBAction readMoreTapped 方法,它在点击 More 按钮时调用:
isReadMoreTapped = 1;
[self.tableView beginUpdates];
[self.tableView endUpdates];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0]; // I need to reload only the third row, so not reloading the entire table but only the required one
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
完成所有这些之后,我确实获得了所需的功能。计算该特定单元格的新高度并将新文本加载到其中。 但是 TableView 上有一个非常不自然的抖动,这会导致糟糕的用户体验。但是,应用商店的更多 按钮并非如此。它的情况下没有不自然的 SCSS 。 TableView 保留在原处,只有更改后的单元格的大小增加,文本显示流畅。
如何才能达到 iPhone 应用商店 更多 按钮的流畅度?
最佳答案
您的问题可能来自重新加载行。您想尝试直接配置单元格属性。我通常使用专用方法来配置我的单元格内容,这样我就不必重新加载行。
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(isReadMoreTapped==0)
cell.label.text = initialText;
else
cell.label.text = finalText;
// all other cell configuration goes here
}
此方法从 cellForRowAtIndexPath 方法调用,它将配置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
您可以直接调用此方法以避免重新加载:
isReadMoreTapped = 1;
[self.tableView beginUpdates];
[self.tableView endUpdates];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:rowToReload];
[self configureCell:cell forRowAtIndexPath:rowToReload];
关于iphone - 动态更改单元格高度时 UITableView 的不自然 SCSS ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541471/