簡體   English   中英

滾動調整UITableView的元素和高度

[英]Adjusting elements and the height of UITableView on scroll

我有一個帶有多個UILabelsUITableView 問題是,當我從服務器接收數據時,這些單元格中的文本會動態更改。 當我加載視圖控制器時,它工作正常。 但是當我滾動時,單元格的高度不會更新,因為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嗎?

謝謝。

確定這里來的編輯答案:問題是你sizeToFitlayoutSubviews 只需點擊此鏈接即可解決您的問題: 此處

如果您還希望單元格和標簽動態調整為相應的文本大小,但同時您的時間標簽位於其正下方,則必須根據文本,字體和字體大小確定uilabel的大小。 有關更多信息,請參見此處: 此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM