Aniruddh
Aniruddh

Reputation: 7668

Add One More Cell to tableView

My issue is similar what this guy had:

Add a last cell to UItableview

In fact I'm using the same method which got selected in that question, here's the code snippet:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    return [array count] + 1;
} 

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        //   cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }

    // Configure the cell.

    NSUInteger rowN = [indexPath row];

    if (rowN == [array count]) {

        cell.textLabel.text = [NSString stringWithFormat:@"Some Text"];

    } else {

    TFHppleElement* pCell = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [pCell content]];

    }

    return cell;    
}

Now, I'm getting exception error: *** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5].

What could be the issue? From the error, it looks like that [array objectAtIndex:indexPath.row] is trying to access 6th index of the array which doesn't exist but why it is trying to access 6th index when I have the code in else condition?

Upvotes: 1

Views: 388

Answers (1)

akoso
akoso

Reputation: 620

Change this row:

NSUInteger rowN = [indexPath row];

to:

NSUInteger rowN = indexPath.row;

Or use just the indexPath.row instead of the rowN in the if condition

Upvotes: 2

Related Questions