[英]Adjusting elements and the height of UITableView on scroll
我有一個帶有多個UILabels
的UITableView
。 問題是,當我從服務器接收數據時,這些單元格中的文本會動態更改。 當我加載視圖控制器時,它工作正常。 但是當我滾動時,單元格的高度不會更新,因為heightForRowAtIndexPath
僅被調用一次。
這是屏幕截圖:
正如我在屏幕快照中所顯示的,問題標簽的大小減小了,從而導致了差距(如箭頭所示)。
這是我的cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifier = @"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
cell.question.autoDetectLinks = YES;
// Used to populate cell from NSDictionary
[self setDataToCell:cell AtIndexPath:indexPath];
return cell;
}
這是我的自定義單元格的layoutSubviews
:
- (void) layoutSubviews
{
CGRect frame = self.question.frame;
frame.size.width = 277.0f; //you need to adjust this value
self.question.frame = frame;
self.question.numberOfLines = 2;
[self.question sizeToFit];
// Place time below question
CGRect timeFrame = self.time.frame;
timeFrame.origin.y = self.question.frame.origin.y + self.question.frame.size.height + 5;
self.time.frame = timeFrame;
[self.time sizeToFit];
}
因此,為了解決這種情況,我打電話給
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[_tableIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
這解決了我的問題,但降低了性能,即使在將動畫設置為UITableViewRowAnimationNone
之后,元素也會在穩定之前跳轉。 有更好的方法嗎? 我應該在其他地方調用reloadRowsAtIndexPaths
嗎?
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.