我是Objective-C和Xcode的新手。我以前写过C程序,所以使用Xcode遇到了很多问题。
现在,我正在编写一个像“instagram”这样的应用程序来显示图像和相关评论。所有这些图像和评论都来自数据库,这在我的问题中并不重要。
所以,请看看我的设计。 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 ;
那么,有人可以建议我如何实现这一目标?或者还有另一种方法可以实现目标吗?谢谢你的帮助。
答案 0 :(得分:2)
我认为你的方式正确。
创建从UITableViewCell
派生的自定义tableview单元格,在单元格的内容视图中添加UIImageView
和UITableView
。在此自定义单元类中添加tableview的委托函数。在创建此自定义单元格实例时,从主视图中,还要为注释表设置数据源。
根据评论编辑
您已经有一个UITableViewController
,其中包含UITableView
,用于显示图片和评论。该类将包含用于处理此数据源的委托方法。让我们将这个类的名称称为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课程中,您将拥有UIImageView
和UITableView
。所以这个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的委托,并使用commentsTableDataSource作为comments表的数据源。希望您了解如何实施。请试一试。
快乐编码!!