-2

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;
}
iOS
  • 423
  • 1
  • 6
  • 15
  • it is crashing on which line and please post the error which you are getting at the time of crash too. – Vaibhav Gautam Sep 03 '13 at 08:57
  • Try Else if not if condition – Bhavesh Nayi Sep 03 '13 at 08:59
  • Please, add the error message you get... (Make sure you have an exception breakpoint set) – Vinzzz Sep 03 '13 at 09:16
  • I am getting the below error. *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]' *** First throw call stack: That issue get resolved. Instead of using table name, i am using tags. Its working now. thanks. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger number; if(tableView.tag == 1) number = [data1 count]; if(tableView.tag == 2) number = [data2 count]; return number; } – iOS Sep 04 '13 at 06:15

2 Answers2

1

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;
}
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
1

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.

Bishal Ghimire
  • 2,580
  • 22
  • 37