0

我正在尝试根据显示的文本调整 UITableViewCell 的高度。我希望完全显示文本。

我在 cellForRowAtIndexPath 中创建了一个带有特定 TEXT_CELL 标签的单元格

if ([rowType isEqualToString:kTextCell] ) {

        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        // Create a text view;
        UITextView *newText = [[UITextView alloc] initWithFrame:CGRectZero];
        newText.backgroundColor = [UIColor blueColor];
        newText.tag = kNotesTag;
        newText.editable = NO;
        newText.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:newText];

        [newText release];
}

当我显示单元格时,我会使用 sizeOfFont 函数计算出显示所有文本所需的大小。然后我将该大小设置为一个变量,该变量在 heightOfCell 函数中使用。

    UITextView *notes = (UITextView*) [cell viewWithTag:kNotesTag];

    if (notes != nil) {
        notes.text = [self.managedObject dispalyValueForKeyPath:rowKey];

        // resize it to the right height          
        CGRect contentFrame = cell.contentView.frame;

        CGSize textSize = [notes.text sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] 
                                 constrainedToSize:CGSizeMake(contentFrame.size.width, CGFLOAT_MAX) 
                                     lineBreakMode:UILineBreakModeWordWrap];   

        CGFloat newHeight = textSize.height + (CELL_CONTENT_MARGIN*2); //some space at the bottom

        CGFloat textHeight = self.storedTextHeight;            

        if (textHeight != newHeight) {

            [notes setFrame:CGRectMake(contentFrame.origin.x, 
                                       CELL_CONTENT_MARGIN+contentFrame.origin.y, 
                                       contentFrame.size.width, newHeight)];

            self.storedTextHeight = newHeight;

            // we reset the height fo the row, so reload it
            [tView beginUpdates];
            [tView endUpdates];  

        }

计算按预期进行,调用 heightForRow 并更新单元格高度。但是,我总是在 [tView endUpdates] 行获得 SIGABT。

错误消息如下。我用谷歌搜索了一堆,但不清楚为什么会这样。不创建或添加单元格。

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (1) 后现有节中包含的行数必须等于其中包含的行数更新前的那个节 (2),加上或减去从该节插入或删除的行数(0 插入,0 删除),加上或减去移入或移出该节的行数(0 移入,0搬出)。'

感谢您对此的任何见解。

4

1 回答 1

1

您的崩溃报告表明问题可能不是源于您的调整大小策略。它说行数正在改变,但您自己并没有改变行数。这是因为您重新加载表格的方式。

要重新加载表格视图,您应该只调用 [tView reloadData] 而不是-beginUpdatesand -endUpdates

如果您使用-beginUpdatesand -endUpdates,表格视图会认为您正在手动更改表格,即使用-deleteRowsAtIndexPaths:withRowAnimation:和批次。如果您不这样做(如果您自己实际上并没有进行任何更新,而实际上您不是),那么您应该调用-reloadData. 如果要使用-beginUpdatesand -endUpdates,则可以在两个调用之间插入:

[tView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:firstIndexPathToDelete, 
    someOtherIndexPathToDelete, maybeOneMoreIndexPathToDelete, nil] 
    withRowAnimation:UITableViewRowAnimationFade];

在调用-beginUpdates和之间-endUpdates

编辑:

为了提高 的性能-reloadData,您需要确保您没有在 中做很多繁重的工作-cellForRowAtIndexPath:,并且您正在重用单元格。网上有很多关于表格视图性能的文章:

UITableView 的 reloadData 有多贵?

tableView 中的 reloadData 使性能变慢

在这里讨论

于 2012-07-16T15:12:51.197 回答