首页 文章

UITableView - 由细胞本身控制的动态细胞高度

提问于
浏览
1

我有以下设置:UITableView与自定义UITableViewCell实现 . 在每个单元格中,我有一个UILabel和一个UIButton . 当首次显示表格时,在每个单元格中,UILabel的行数设置为1,所有单元格都有固定的高度(在我的情况下为60 px) . 当点击单元格中的按钮时,UILabel的行数设置为0并打开自动换行,有效地扩展了表格单元格 .

现在,问题是只有UITableViewCell的实现知道标签是否被扩展 - 因此细胞高度应该是什么 . 但是,所有者文件是表的数据源 .

我无法理解如何设置它 . 任何想法都表示赞赏 .

Update 这里是我的代码摘录

在自定义UITableViewCell实现中:

- (float)requiredHeight
{
    if(isFull)
    {
        CGSize labelSize = [LblTitle.text sizeWithFont: [LblContent font]
                                     constrainedToSize: CGSizeMake(300.0f, 300.0f) 
                                         lineBreakMode: UILineBreakModeTailTruncation];
        return 42.0f + labelSize.height;
    }
    else
    {
        return 60.0f;
    }
}

在所有者文件(UITableViewDelegate)中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OOCommentCell *cell = (OOCommentCell*)[tableView cellForRowAtIndexPath:indexPath];
    return [cell requiredHeight];
}

然而,这会在无限循环或BAD_ACCESS_EXCEPTION中随机产生 .

2 回答

  • 2

    好吧,经过一番挣扎,这就是我最终得到的,哪种作品 .

    • 我创建了一个简单的类来包含与一个单元格相关的信息:

    @interface CommentInfo:NSObject
    {
    int ID;
    NSString *名称;
    NSString *日期;
    NSString *内容;
    BOOL IsFull;
    浮高;
    }

    @property(readonly)int ID;
    @property(readonly)NSString * Name;
    @property(readonly)NSString * Date;
    @property(readonly)NSString *内容;
    @property(readwrite,assign)float高度;
    @property(readwrite,assign)BOOL IsFull;

    • (id)initWithID:(int)id withName:(NSString *) name withDate:(NSString *)_ date withText:(NSString *)_ text;

    @结束

    不要过分担心所有属性 - 最重要的是 Height .

    • 在我的控制器(也是表视图的委托)中,我将数据保存为指向 CommentInfo 类型对象的 NSMutableArray 指针 .

    • cellForRowAtIndexPath 中,我得到了相应的指针,并在构造期间将其传递给自定义单元格实现,并在其中存储 . 我还将 self 设置为单元格的委托 .

    • 在自定义单元格实现中,当我需要展开/更改高度时,我更新 CommentInfo 对象中的 Height 属性并调用委托中的方法来更新显示 .

    • 当调用此 updateDisplay 方法时,我会执行以下操作:

    [CommentsTable beginUpdates];
    [CommentsTable endUpdates];

    • heightForRowAtIndexPath 方法中,我检索到 CommentInfo 的相应指针并读取 Height 属性 . 由于控制器和单元格之间的指针是相同的,因此在两个类中都可以看到对此属性的任何更改 .

    任务完成 .

  • 0

    在表委托中实现方法UITableViewDelegate – tableView:heightForRowAtIndexPath: . 如果要基于每个单元格定义高度,可以通过在tableView:heightForRowAtIndexPath中调用自己的tableView:cellForRowAtIndexPath:方法来调用单元格的类 .

    更新:在代码中实现 . 该错误是由委托调用tableView:cellForRowAtIndexPath中的方法签名不正确引起的 .

    在自定义UITableViewCell实现中:

    - (CGFloat)requiredHeight
    {
        if(isFull)
        {
            CGSize labelSize = [LblTitle.text sizeWithFont: [LblContent font]
                                         constrainedToSize: CGSizeMake(300.0f, 300.0f) 
                                             lineBreakMode: UILineBreakModeTailTruncation];
            return 42.0f + labelSize.height;
        }
        else
        {
            return 60.0f;
        }
    }
    

    在所有者文件(UITableViewDelegate)中:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        OOCommentCell *cell = (OOCommentCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return [cell requiredHeight];
    }
    

相关问题