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

时间:2016-10-26 21:58:53

标签: ios objective-c uitableview sdwebimage nsrangeexception

所以在我的表视图中,我有一个名为dataArray的数组,它使用JSON从服务器获取其对象。我为每个单元格添加了一个按钮,当单击该按钮时,单元格应该移动到另一个部分/或另一个名为followArray的数组,以便更精确。现在细胞正在转移到另一个区域,但在移动6个细胞后,我得到一个错误,说明了这一点。所有数组都是NSMutableArray。另外,我是iOS的新手,所以请尝试与我合作,谢谢。

  

2016-10-26 17:27:19.569 CustomCellApp [3212:198103] *因未捕获的异常终止应用' NSRangeException',原因:' * - [__NSArrayM objectAtIndex:]:索引6超出界限[0 .. 4]'

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

旁注,我可以发布一个单独的错误帖子,但我有另一个问题,在切换部分时,使用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 ----- //
 }
}

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

1 个答案:

答案 0 :(得分:0)

这就是帮我修复错误的原因。感谢@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];