I am trying to count my array to set as the number of rows in my table and then multiply it by 2 because I have an invisible cell in between each cell to make the cells look separated. Every time I try to build, I get this error:
Terminating app due to uncaught exception ‘NSRangeException’, reason:
‘* -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]’
This is the code I am using:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [xmlParser.calls count] * 2;
}
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]]];
if (indexPath.row % 2 == 1) {
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO];
[cell2 setBackgroundColor:[UIColor clearColor]];
}
return cell2;
}
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [@"Station: " stringByAppendingString:currentCall.station];
cell.contentView.layer.backgroundColor = [UIColor whiteColor].CGColor;
cell.contentView.layer.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
cell.contentView.layer.borderWidth = 1.0;
cell.contentView.layer.borderColor = [UIColor blackColor].CGColor;
if ([currentCall.county isEqualToString:@"W"]) {
cell.countyImageView.image = [UIImage imageNamed:@"blue.png"];
} else {
cell.countyImageView.image = [UIImage imageNamed:@"green.png"];
}
if ([currentCall.callType isEqualToString:@"F"]) {
cell.callTypeImageView.image = [UIImage imageNamed:@"red.png"];
} else {
cell.callTypeImageView.image = [UIImage imageNamed:@"yellow.png"];
}
return cell;
}
EDIT:
Made a closer look. Just add the
/2:And it’s going to work.
Just noticed the same answer below from
richard.As mayuur noticed:
in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section:And then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:So you should remove multiply by 2 or suggest something by your own, you’ll always going to get this error unless xmlParser.calls is an empty array.