【问题标题】:How to implement a tableview inside a custom tableview cell?如何在自定义表格视图单元格中实现表格视图?
【发布时间】:2013-07-10 07:30:28
【问题描述】:

我是 Objective-C 和 Xcode 的新手。我以前写过C程序,所以在使用Xcode时遇到了很多问题。

现在,我正在编写一个像“instagram”这样的应用程序,它可以显示图像和相关的 cmets。所有这些图像和 cmets 都来自数据库,这在我的问题中并不重要。

所以,请看看我的设计。 Click me

这是“一个单元格设置”,应用程序在向下滚动并显示另一个单元格时会不断显示不同的图像和评论。

首先,我创建一个UITableViewController,然后将表格视图单元格放大到整个视图。我在单元格上添加了一个UIImageView 和表格视图,因此,这是一个自定义单元格。

然后我创建一个UITableViewCell 类来实现这个自定义单元格。我搜索并发现我可以在这个UITableViewCell 类中添加一个子视图,以在我的自定义单元格中显示UIImage,例如:

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x,     cell.contentView.frame.size.height, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];

但我不知道如何将UITableView 添加为我的自定义单元格的子视图。说,我可以加个代码

[cell.contentView addSubView:tableView] ;

到我的自定义单元格类中,但我可以在哪里配置这个嵌入式TableView 的单元格内容?因此这种方法:

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ;

那么,有人可以建议我如何实现这一目标吗?还是有另一种方法可以实现目标?感谢您的帮助。

【问题讨论】:

    标签: uitableview custom-cell


    【解决方案1】:

    我认为你的方法是正确的。

    创建一个从UITableViewCell 派生的自定义表格视图单元格,在单元格的内容视图中添加UIImageViewUITableView。在这个自定义单元类中添加 tableview 的委托函数。当您创建此自定义单元格的实例时,从您的主视图中,也为您的 cmets 表设置数据源。

    基于 cmets 编辑

    您已经有一个UITableViewController,其中包含一个UITableView,用于与cmets 一起显示图像。此类将包含用于处理此数据源的委托方法。让我们说这个类的名称为 ContentViewController。

    在本课程中,您将拥有cellForRowAtIndexPath 来处理实际数据源,如下所示:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellId= @"CellId";
        UITableViewCell *cell = nil;
        yourDataObj = [yourDataSource objectAtIndex:indexPath.row];
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil){
           cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId:yourDataObj]autorelease];
        }
        //Do other cell updations here
        // yourDataObj is a custom class which conatins an array to hold your comments data.
    }
    

    现在在您的 CustomCell 类中,您将拥有 UIImageViewUITableView。所以这个 CustomCell 类的 init 方法将如下所示。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier :(YourDataObj*)cellDisplayData{
    
         if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    
              //You need to alloc and init your imageView and tableView here.
              self.imageView.image = cellDisplayData.image;// or image url whatever
              self.commentsTableDataSource = cellDisplayData.comments; //commentsTableDataSource is property holding comments array
    
              self.commentsTable.delegate = self;
              self.commentsTable.dataSource = self;
              [self.commentsTable reloadData]; 
         }
     }
    

    此外,现在在这个自定义单元类中,您可以添加 tableview 的委托并使用 cmetsTableDataSource 作为 cmets 表的数据源。希望您对如何实施有所了解。请试一试。

    编码愉快!!

    【讨论】:

    • 首先感谢您的回复。但我不太明白“当你创建这个自定义单元格的实例时,从你的主视图中,也为你的 cmets 表设置数据源”,我应该在哪里添加“内部”表视图的数据源和委托?
    • 在您的自定义单元格类中。您可以在自定义单元格类 .h 文件中添加 UITableViewDelegate 协议,并在其中添加 cellForRowAtIndexPath 和其他委托方法。
    • 你能给我举个例子吗?这是否意味着我可以在一个定制的单元格类 .h 文件上拥有多个 tableview 委托和数据源方法,例如 cellForRowAtIndexPath?
    • 编辑了答案..看看。
    • 感谢您的大力帮助,让我尝试实现它。