views:

72

answers:

2

I am trying to add a UITableViewCell at the very bottom of my UITableView programatically. I am getting an index out of bounds error, which I am not sure how to resolve:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [items count]+1;
}

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

    static NSString *CellIdentifier = @"Cell";

    if (indexPath.row == [items count] + 1) {
        UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MoreCell"] autorelease];
        cell.textLabel.text = @"More...";
        return cell;
    }
    else {
        STUser *mySTUser;
        mySTUser = [items objectAtIndex:indexPath.row]; //!!INDEX OUT OF BOUNDS HERE!!

        AsyncImageView* asyncImage = nil;
        UILabel* loginLabel = nil;
        UILabel* fullNameLabel = nil;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            CGRect frame = CGRectMake(0, 0, 44, 44);
            CGRect loginLabelFrame = CGRectMake(60, 0, 200, 10);
            CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 10);
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
            asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease];
            [cell.contentView addSubview:asyncImage];
            loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease];
            [cell.contentView addSubview:loginLabel];
            fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease];
            [cell.contentView addSubview:fullNameLabel];    
            asyncImage.tag = IMAGE_TAG;
            loginLabel.tag = LOGIN_TAG;
            fullNameLabel.tag = FULL_NAME_TAG;      
        }
        else {
            asyncImage = (AsyncImageView *) [cell.contentView viewWithTag:IMAGE_TAG];
            loginLabel = (UILabel *) [cell.contentView viewWithTag:LOGIN_TAG];
            fullNameLabel = (UILabel *) [cell.contentView viewWithTag:FULL_NAME_TAG];
        }

        // Configure the cell...

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        CGRect frame = CGRectMake(0, 0, 44, 44);
        asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease];
        NSURL* url = [NSURL URLWithString:mySTUser.avatar_url_large];
        [asyncImage loadImageFromURL:url];
        asyncImage.tag = IMAGE_TAG;
        [cell.contentView addSubview:asyncImage];
        CALayer * l = [asyncImage layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:5.0];

        CGRect loginLabelFrame = CGRectMake(60, 0, 200, 20);
        loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease];
        loginLabel.text = [NSString stringWithFormat:@"%@",mySTUser.login];
        loginLabel.tag = LOGIN_TAG;
        [cell.contentView addSubview:loginLabel];

        CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 20);
        fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease];
        fullNameLabel.text = [NSString stringWithFormat:@"%@ %@",mySTUser.first_name, mySTUser.last_name];
        fullNameLabel.tag = FULL_NAME_TAG;
        fullNameLabel.font = [UIFont fontWithName:@"Helvetica" size:(12.0)];
        [cell.contentView addSubview:fullNameLabel];    

        return cell;
    }
}

If the count is items + 1 I want to return the More Cell, if not, then I want it to create all of the other cells. Not sure why I get the index out of bounds since the code should never reach that point.

A: 

Arrays in Objective-C and almost all other programming languages are zero-based. That means the first object has index 0, and the last object has index (length - 1).

Therefore, the condition

if (indexPath.row == [items count] + 1) {

should be changed to

if (indexPath.row == [items count]) {
KennyTM
+1  A: 

indexPath.row is zero based.

Change:
if (indexPath.row == [items count] + 1) {
To:
if (indexPath.row == [items count]) {

progrmr