单元格切换部分时 tableview 中的错误

Error within tableview when cells are switching sections

所以在我的 table 视图中,我有一个名为 dataArray 的数组,它使用 JSON 从服务器获取其对象。我向每个单元格添加了一个按钮,当单击该按钮时,该单元格应该移动到另一个部分/或另一个名为 followedArray 的数组,更准确地说。现在单元格正在转移到另一个部分,但在移动 6 个单元格后,我收到一条错误消息。所有的数组都是NSMutable数组。另外,我是 iOS 的新手,所以请尝试与我合作,谢谢。

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]'

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

旁注,我可以单独出错 post 但我还有另一个问题,使用 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 ----- //
 }
}

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

这是帮助我修复我遇到的错误的原因。感谢@AliOmari 帮助我。

在 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;

在我的按钮里面

    [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];