在NSMutableArray之间移动对象时出错

时间:2016-10-24 05:10:05

标签: ios objective-c uitableview nsmutablearray

在我的iOS应用程序中,我有一个表视图,它有2个部分/ 2个数组。我试图将一个对象从一个数组/部分移动到另一个数组/部分。现在,第0部分称为followArray,第1部分称为dataArray,dataArray存储构成每个单元的所有数据。因此,当用户单击我设置的名为Follow的按钮时,它应该接受该单元格并从dataArray / Section 1中删除它并将其插入followArray / Section 0。但是当我尝试这样做时,我得到一个错误这样说。

错误消息

  

***断言失败 - [UITableView _endCellAnimationsWithContext:],/ BuildRoot / Library / Cache / com.apple.xbs / Sources / UIKit / UIKit-3599.6 / UITableView.m:1396

     

***因未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试将第0行插入第0部分,但更新后第0部分只有0行'

代码:

的TableView

- (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 Button
    cell.followButton.tag = indexPath.row;
    [cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.followButton.hidden = NO;

    return cell;
}

章节标题

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

行数

- (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];
}

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

- 这是行动发生的地方 -

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

关注按钮

-(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)
    {
        NSLog(@"Current Row = %@", indexPath);


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

    // ----- Inserting Cell to Section 0 ----- *NOT WORKING*
    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
    NSInteger rowToAdd = indexPath.row;
    [self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

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

    [self.myTableView endUpdates];

    }
}

3 个答案:

答案 0 :(得分:1)

修改跟随按钮代码如下所述:

关注按钮

-(void)followButtonClick:(UIButton*)sender {
    //Use this IndexPath
    NSInteger buttonTag = [sender tag];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:<YourSection>];

    // Creating an action per tag
    if (indexPath != nil)
    {
        NSLog(@"Current Row = %@", indexPath);


        // ----- ERROR HERE -----

        [self.myTableView beginUpdates];

        // ----- Inserting Cell to Section 0 -----
        [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
        NSInteger rowToAdd = indexPath.row;
        [self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

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

        [self.myTableView endUpdates];

    }

}

答案 1 :(得分:0)

这可能是因为您使用convertPoint来获取所单击单元格的indexPath。尝试在CustomCell中创建协议,并在CustomCell中实现followButtonClick,并使用委托通过CustomCell对象获取控制器中的callBack,并使用该对象获取indexPath。请参考以下代码。

您的CustomCell.h     #import

@protocol CustomCellDelegate;

@interface CustomCell : UITableViewCell

@property(nonatomic,weak) id<CustomCellDelegate> delegate;

@end

@protocol CustomCellDelegate <NSObject>

-(void)followButtonClickedWithCell:(CustomCell *) cell;

@end

您的CustomCell.m

#import "CustomCell.h"

@implementation CustomCell
...
-(void)followButtonClicked:(UIButton *) sender{
    if(self.delegate != nil){
        [self.delegate followButtonClickedWithCell:self];
    }
}

...
@end

确认您的视图控制器实现CustomCellDelegate,并在委托方法中引用以下代码以获取单击的indexPath

-(void)followButtonClickedWithCell:(CustomCell *) cell{
    NSIndexPath *clickedIndexPath = [self.tableView indexPathForCell:cell];
    if (clickedIndexPath != nil){
        //Implement your logic here
    }
}

答案 2 :(得分:0)

这是帮我修复错误的原因,致@AliOmari,@ Rachel&amp; @CodeChanger

    [self.myTableView beginUpdates];

    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

    [dataArray removeObjectAtIndex:indexPath.row];
    NSInteger rowToRemove = indexPath.row;
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    [self.myTableView endUpdates];