new2ios
new2ios

Reputation: 1350

How to configure UITableView inside custom UITableViewCell in a Storyboard?

I have a layout in which I will have 2 UITableViews with custom cells. The second UITableView must be inside the first.

My question is: how to delegate second UITableView?

Can I delegate both to my ViewController? In that case it will use the same methods and I have to find out which UITableView is managed right now.

Or I have to delegate it inside custom UITableViewCell of the first UITableView?

Any recommendations are appreciated.

EDIT: I don't know how to implement solutions here, because I have Storyboard. Inside my current UIViewController I set delegate and dataSource of the first UITableView to my View Controller.

My problem is that I don't know how to set the same properties of the second Table View (which will be inside UITableViewCell). I can not set them to UITableViewCell (IB does not allow that).

Where and how to set then in the IB?

Upvotes: 0

Views: 1093

Answers (3)

JordanMazurke
JordanMazurke

Reputation: 1123

A far better solution would be to abstract the DataSource and Delegate implementations away from your view controller so that they can be personalised per tableview as required (please note that the code is taken from the objc.io article Lighter View Controllers.

E.g.

@implementation ArrayDataSource

- (id)itemAtIndexPath:(NSIndexPath*)indexPath {
    return items[(NSUInteger)indexPath.row];
}

- (NSInteger)tableView:(UITableView*)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return items.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView 
        cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                              forIndexPath:indexPath];
    id item = [self itemAtIndexPath:indexPath];
    configureCellBlock(cell,item);
    return cell;
}

@end

Then you could utilise it as follows:

void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
   cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
                                                cellIdentifier:PhotoCellIdentifier
                                            configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;

The same process could be followed with the UITableViewDelegate implementations to provide you with a very clean, separated and de-coupled code base. Your requirement for two tableviews will then be intrinsically easier to implement.

Upvotes: 3

Dharmesh Dhorajiya
Dharmesh Dhorajiya

Reputation: 3984

You just check table condition for every delegate method

Use this code to register custom cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.yourFirstTable)
{
    CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellModifier"];
    // your code
}
else
{
    // second table cell code
}
return cell;
}



 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   { 
        if(tableView == self.yourFirstTable)
        {
             // first tableView number of row return
        }
        else
        {
             // second table number of row return
        }   
   }

And create prototype cell in TableView

enter image description here

And Set CellReusableId like this way

enter image description here

Upvotes: 1

user3182143
user3182143

Reputation: 9589

My answer is

  For identifying two table view data source and delegate method is,better to set tag for the table views.

Set this below coding in your tableview delegates method.

 if(tableView.tag==0)
 {
 }
 else
 {
 }

Also you can vary this by assigning different name to these table view.

 if(tableView==FirstTableView)
 {
 }
 else
 {
 }

Upvotes: 2

Related Questions