【问题标题】:UITableView with Various Custom Cells of differing heights within UIViewUITableView 与 UIView 中不同高度的各种自定义单元格
【发布时间】:2013-11-08 07:41:15
【问题描述】:

我正在构建一个 IOS 应用主页,它基本上由以下结构组成 -

这基本上是一个 UIView,其中包含一个嵌套的 UIScrollView,而后者又包含一个带有 3 个自定义单元格的 TableView。

我从动态数组中提取数据并将其过滤到相关单元格中 - 除了我无法解决的两个问题之外,所有工作正常 -

1- 自定义单元格在情节提要中具有不同的高度,但是在编译应用程序时它们始终是相同的高度 - 是否可以在 UITableView 行上设置自动高度?如果没有,谁能解释我如何将正确的高度应用于每个单元格?

2- 包装表格视图的 TableView / View 需要扩展以使所有动态单元格可见 - 我该如何实现?

下面是我的tableview方法供参考-

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"CellFeed";
    static NSString *CellIdentifierArtNo =@"CellArtRecNo";
    static NSString *CellIdentifierBook =@"CellBooking";
    UITableViewCell *cell;
    feedData *f = [self.HpFeedArray objectAtIndex:indexPath.section];

    NSString * ArtPString = @"articleP";
    NSString * ArtNoPString = @"article";
    NSString * ArtBook = @"booking";

    if([f.FeedGroup isEqualToString:ArtPString]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  
                                               forIndexPath:indexPath];
        CellHp_RecArticleWithImage *cellImage = (CellHp_RecArticleWithImage *)cell;
        cellImage.selectionStyle = UITableViewCellSelectionStyleNone;
        cellImage.artTitle.text = f.FeedTitle;
        cellImage.artImg.text = f.FeedDesc;
        cellImage.artDate.text = f.FeedDate;
        cellImage.textLabel.backgroundColor=[UIColor clearColor];

        return cellImage;
    }
    if([f.FeedGroup isEqualToString:ArtBook]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierBook 
                                               forIndexPath:indexPath];
        CellHp_BookingAlert *cellBook = (CellHp_BookingAlert *)cell;
        cellBook.selectionStyle = UITableViewCellSelectionStyleNone;
        cellBook.HeadlineLbl.text = f.FeedTitle;
        cellBook.TextBoxLbl.text = f.FeedDesc;
        //cellBook.DateLbl.text = f.FeedDate;

        return cellBook;
    }
    else{
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierArtNo 
                                               forIndexPath:indexPath];
        CellHp_RecArticleNoImage *cellNoImage = (CellHp_RecArticleNoImage *)cell;
        cellNoImage.selectionStyle = UITableViewCellSelectionStyleNone;
        cellNoImage.artTitle.text = f.FeedTitle;
        cellNoImage.artImg.text = f.FeedDesc;
        cellNoImage.artDate.text = f.FeedDate;
        cellNoImage.textLabel.backgroundColor=[UIColor clearColor];

        return cellNoImage;
    }

干杯

【问题讨论】:

    标签: ios objective-c xcode uitableview


    【解决方案1】:

    看起来您有自定义 UITableViewCell 子类,例如 CellHp_RecArticleNoImageCellHp_BookingAlert(我可以建议将它们重命名为不那么令人头疼的东西吗?;))。

    在这种情况下,如果您只有 3 种不同的细胞类型,您可以这样做:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
    {
        UITableViewCell* cell = [tableView cellForAtIndexPath:indexPath];
    
        if ([cell isKindOfClass:[CellHp_BookingAlert class])
        {
             return 123.0;
        }
        else if ([cell isKindOfClass:[CellHp_RecArticleNoImage class])
        {
             return ...
        }
         ...
    }
    

    或者,您可以让所有自定义类实现height 方法以获得更简洁的解决方案,并且在heightForRowAtIndexPath: 中您可以获取单元格并调用其高度方法。

    回复。问题 2,使用 Tim 提到的UIView's sizeToFit method

    【讨论】:

      【解决方案2】:

      您需要实现以下方法来识别单元格的高度。

      - (CGFloat)tableView:(UITableView *)tableView 
        heightForRowAtIndexPath:(NSIndexPath 
         *)indexPath;
      

      【讨论】:

      • 感谢@hussain,如何应用单元格的高度和周围视图的高度?
      【解决方案3】:
      1. 您可以根据此处概述的过程,通过在 heightForRowAtIndexPath 中对它们进行出队来从情节提要中导出静态或动态(数据驱动)单元格高度:Retrieve custom prototype cell height from storyboard?

      2. 我不确定我是否理解这个问题,但如果您尝试调整表格大小以适应内容,则可以获取表格视图的contentSize

      【讨论】:

        猜你喜欢
        • 2020-10-16
        • 2011-11-27
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多