iosobjective-cuitableviewsdwebimagensrangeexception

Error within tableview when cells are switching sections


So in my table view, I have an array called dataArray and it's getting its objects from a server using JSON. I added a button to each cell and when the button is clicked the cell is supposed to move to another section/ or another array called followedArray to be more exact. Now the cells are transferring to the other section but after moving 6 cells I get an error that saids this. All arrays are NSMutableArray. Also, I'm new to iOS so try to work with me, thank you.

2016-10-26 17:27:19.569 CustomCellApp[3212:198103] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 4]'

---------------------

Side note, I can make a separate error post but I have another issue, the images displaying in the cells are being changed to other images when switching sections, using the pod SDWebImage.

---------------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (!isFiltered) {

    if (section == 0) {
        return [followedArray count];
    }
    else if (section == 1) {
        return [dataArray count];
    }
 }
return [filteredArray count];

}

---------------------

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
    return @"Followed Data";
}
else {
    return @"All Data";
 }
}

---------------------

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

static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configuring the cell
Data * dataObject;
if (!isFiltered) {

    if (indexPath.section == 0) {
        dataObject = [followedArray objectAtIndex:indexPath.row];
    }
    else if (indexPath.section == 1) {
        dataObject = [dataArray objectAtIndex:indexPath.row];
    }
}
else {
    dataObject = [filteredArray objectAtIndex:indexPath.row];
}

// Loading Images
if (!isFiltered) {
  // Exception Breakpoint Here
    NSURL * imgURL = [[dataArray objectAtIndex:indexPath.row] valueForKey:@"dataURL"];
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.png"] options:SDWebImageRefreshCached];
}else{
    NSURL * imgURL = [[filteredArray objectAtIndex:indexPath.row] valueForKey:@"dataURL"];
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.png"] options:SDWebImageRefreshCached];
}

// Loading Follow Button
cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;

 return cell;
}

---------------------

-(void)followButtonClick:(UIButton *)sender {

// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

// Creating an action per tag
if (indexPath != nil)
{

    // Change Follow to Following
    [sender setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
    cell.followButton.hidden = YES;
    cell.followedButton.hidden = NO;


    // ----- ERROR BEGINS HERE ----- //
    [self.myTableView beginUpdates];

    // ----- Inserting Cell to Section 0 ----- *CAUSING PROBLEMS*
      // Exception Breakpoint Here
    [followedArray addObject:[dataArray objectAtIndex:indexPath.row]];
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:followedArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

    NSLog(@"indexPath.row = %ld", (long)indexPath.row);

    // ----- Removing Cell from Section 1 ----- *WORKING*
    [dataArray removeObjectAtIndex:indexPath.row];
    NSInteger rowToRemove = indexPath.row;
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    NSLog(@"Array =%@",followedArray);

    [self.myTableView endUpdates];
    // ----- ERROR ENDS HERE ----- //
 }
}

---------------------


Solution

  • This is what helped me fix the errors I was having. Credits to @AliOmari for helping me out.

    In cellForRowAtIndexPath

    // Configuring the cell
    Data * dataObject;
    if (!isFiltered) {
    
        if (indexPath.section == 0) {
            dataObject = [followedArray objectAtIndex:indexPath.row];
            [cell populateCell:dataObject isFollowed:YES indexPath:indexPath parentView:self];
        }
        else if (indexPath.section == 1) {
            dataObject = [dataArray objectAtIndex:indexPath.row];
            [cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self];
        }
    }
    else {
        dataObject = [filteredArray objectAtIndex:indexPath.row];
        [cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self];
    }
    return cell;
    

    Inside my Button

        [self.myTableView beginUpdates];
    
        // ----- Inserting Cell to Section 0 -----
        [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
        [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        //NSLog(@"indexPath.row = %ld", (long)indexPath.row);
    
        // ----- Removing Cell from Section 1 -----
        [dataArray removeObjectAtIndex:indexPath.row];
        NSInteger rowToRemove = indexPath.row;
        [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
        //NSLog(@"Array =%@",followedArray);
    
        [self.myTableView endUpdates];