Reputation: 79
I am new to Objective-C and Xcode. I used to write C programs before, so I am encountering many problems using Xcode.
Now, I am writing an app like "instagram" that shows image and related comments. All these images and comments are from database, which is not important in my question.
So, please have a look at my design. Click me
This is "one cell setting", at which the apps will keep showing different image and comment while scrolling down and display the another cells.
Firstly, I create a UITableViewController
, then I enlarge the table view cell to whole view. And I add an UIImageView
and table view "onto" the cell, thus, this is a custom cell.
Then I create a UITableViewCell
class to implement this custom cell. I search and found that I can add a subview in this UITableViewCell
class to show the UIImage
in my custom cell like:
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];
But I can't figure out how to add a UITableView
as a subview of my custom cell. Say, I can add a code
[cell.contentView addSubView:tableView] ;
onto my custom cell class, but where can I config the cell contents of this embedded TableView
? Thus this method:
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ;
So, can someone suggest how can I achieve this? Or there is another approach that can achieve the goal? Thanks for the help.
Upvotes: 1
Views: 3279
Reputation: 2127
I think you are going in the right way.
Create a custom tableview cell derived from UITableViewCell
, add a UIImageView
and UITableView
in the cell's content view. Add tableview's delegate functions in this custom cell class. From your main view when you create an instance of this custom cell, set the datasource for your comments table also.
EDIT based on comments
You already have a UITableViewController
which contains a UITableView
for displaying images along with comments. This class will contains the delegate methods for handling this datasource. Lets say the name of this class as ContentViewController.
Here in this class you will have cellForRowAtIndexPath
to handle the actual datasource as follows:
- (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.
}
Now in your CustomCell class, you will have UIImageView
and UITableView
. So the init method of this CustomCell class will look like this.
- (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];
}
}
Also, now in this custom cell class, you can add tableview's delegates and use commentsTableDataSource as your datasource for comments table. Hope you get an idea on how to implement. Please give a try.
Happy Coding!!
Upvotes: 2