load 2 tableviews in a single view iOS

1.7k views Asked by At

I have 2 tableviews (table1,table2) in a view controller. I have 2 data arrays (dataArray1, dataArray2). I want to load the table views with corresponding data arrays i.e.(table1 = dataArray1, table2 = dataArray2).I am trying the below code. But app is crashed? What's wrong in this code? Any help would be appreciated. Please don't suggest to use 2 view controllers.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.table1)
        return [dataArray1 count];
    if(tableView == self.table2)
        return [dataArray2 count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }
    if(tableView == self.table1)
    {
        cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];
    }
    if(tableView == self.table2)
    {
        cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
    }
    return cell;
}
2

There are 2 answers

0
Lithu T.V On

Provide 2 different cell identifiers for the two tableviews

Because both table cells are different and they should be reused ,for maintainig the reuse correctly unique identifier requires to be seperate

Try this

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

    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";

    UITableViewCell *cell;

    if(tableView == self.table1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier1];
        }
        cell.textLabel.text =[dataArray1 objectAtIndex:indexPath.row];

    }
    if(tableView == self.table2)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier2];
        }
        cell.textLabel.text =[dataArray2 objectAtIndex:indexPath.row];
    }
    return cell;
}
2
Bishal Ghimire On

Normally you dont create two table view in one view. But rather create a sections. Even then if u still need two table view its better to use View Class and make separate delegates for them. I have one code https://github.com/bishalg/BGRadioList Which shows you the use of view with table view. Hope it is helpful to you.