Question

I have to two table view in my view if i am load only one table view it is working fine. But when i am trying to load both table view using below method it is giving below exception.

uncaught exception 'NSRangeException', reason: '* -[NSArray objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

- (void)viewDidLoad {
[super viewDidLoad];

array1 = [[NSArray alloc]initWithObjects:@"Start",@"End",@"Frequency",@"Time of Day",nil];
array2 =[[NSArray alloc]initWithObjects:@"Alarm",@"Tone",nil];

table1.scrollEnabled =NO;
table2.scrollEnabled =NO;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == table1) ;
   return 1;

if (tableView == table2); 
    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.table1) ;
    return [array1 count];
if (tableView == self.table2) ;
    return [array2 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] autorelease];
}

// Configure the cell...


if (tableView == self.table1){
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];     

}
if (tableView == self.table2){
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];     

}
return cell;}
Was it helpful?

Solution

You probably request an object at index bigger than one of your arrays has items. Did you implement correctly – numberOfSectionsInTableView: and – tableView:numberOfRowsInSection: method checking for which table are they called and returning appropriate values according to your data arrays?
UPDATE
Edit the methods this way:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    if (tableView == self.table1)
       return 1;

    if (tableView == self.table2)
       return 1;

    return 0;
}

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

    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top