简体   繁体   English

从tableView中删除行

[英]Delete row from tableView

I am a bit lost on what should be a simple action of deleting a row from a tableview. 我对从tableview中删除一行的简单操作感到有点迷茫。 I have an sqlite database with a table called SUBJECT and I want to delete a record for this table, delete the member from the array and delete the entry in the tableview. 我有一个带有一个名为SUBJECT的表的sqlite数据库,我想删除该表的记录,从数组中删除该成员并删除tableview中的条目。 I don't know what sequence is best and I keep getting errors. 我不知道什么序列是最好的,我一直在收到错误。

Here is the code: 这是代码:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];

        //Get the object to delete from the array.
        Subject *sub = [self.subjects objectAtIndex:indexPath.row];

        // check if there are any quoteMaps for this subject and if so do not do it

        NSArray *qm = [appDelegate getQuoteMapsFromSubId:sub.subject_id];

        if (qm.count==0){

            //Delete the object from the table.
            [self.subjects removeObjectAtIndex:indexPath.row];
            [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [sub deleteSubject];


        } else {
            //DON'T DO IT BUT GIVE A WARNING INSTEAD

        }
    }
}

I get the following error on the self.subjects removeObject.... line: 我在self.subjects removeObject .... line上得到以下错误:

-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6b2a8f0'

The error message says my self.subjects is an NSArray yet my header assigns it as NSMutableArray: 错误消息说我的self.subjects是一个NSArray但我的标题将它指定为NSMutableArray:

@interface SubjectViewController : UITableViewController <UIAlertViewDelegate>  {

    NSMutableArray *subjects;

}

@property (nonatomic, retain) NSMutableArray *subjects;

Why is it changing to NSArray??? 为什么要改为NSArray ???

I checked and found that the Parent ViewController is assigning the following: 我检查并发现Parent ViewController正在分配以下内容:

NSPredicate *catFilter = [NSPredicate predicateWithFormat:@"category_title == %@", cat.category_title];

NSArray *subs = [appDelegate.subjects filteredArrayUsingPredicate:catFilter];
 subViewController.subjects = (NSMutableArray *) subs;

Looks like the problem is there. 看起来问题就在那里。

I get the following error if I comment out the self.subjects removeObject and try to run the next line of "tv deleteRowsAtIndexPaths...": 如果我注释掉self.subjects removeObject并尝试运行下一行“tv deleteRowsAtIndexPaths ...”,我会收到以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid update: invalid number of rows in section 0.  The number of rows
contained in an existing section after the update (1) must be equal to the number 
of rows contained in that section before the update (1), plus or minus the number 
of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or 
minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

So far this was narrowed down to being a problem in the prior controller view where I am using an NSArray out of a NSPredicate because I couldn't get NSPredicate to work with NSMutableArray. 到目前为止,这已经缩小到以前的控制器视图中的一个问题,我在NSPredicate中使用NSArray,因为我无法让NSPredicate与NSMutableArray一起工作。 So now that is the issue trying to be resolved. 所以现在这是试图解决的问题。

Or converting/assigning an NSArray as an NSMutableArray... 或转换/分配NSArray作为NSMutableArray ...

NSArrays are inmmutable, that means you can't add or remove elements, hence your object doesn't have a removeObjectAtIndex: method. NSArrays是不可变的,这意味着您无法添加或删除元素,因此您的对象没有removeObjectAtIndex:方法。

  • Check your property to see if its an NSArray instead of an NSMutableArray . 检查您的属性以查看它是否是NSArray而不是NSMutableArray
  • Check that the object in that pointer is in fact an NSMutableArray . 检查该指针中的对象实际上是否为NSMutableArray
  • To assign an NSArray to your variable first create an NSMutableArray from it using the arrayWithArray: method. 要将NSArray分配给变量,首先使用arrayWithArray:方法从中创建NSMutableArray

first your subjects array is immutable. 首先你的主题数组是不可变的。 use NSMutableArray class for subject array. 对主题数组使用NSMutableArray类。

also try to use beginUpdates - endUpdates block... 也尝试使用beginUpdates - endUpdates块...

[tv beginUpdates];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tv endUpdates];

The error is being generated because your datasource, always has to be consistent with the rows added/deleted, which means if the previous call to the method: 正在生成错误,因为您的数据源始终必须与添加/删除的行保持一致,这意味着如果先前调用该方法:

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

Returned x, and you deleted n rows, then the next call to the method after the delete animation MUST return (xn) 返回x,你删除了n行,然后在删除动画之后下一次调用方法必须返回(xn)

Additionally if you are using an NSArray as your datasource and you will be adding/deleting rows then it MUST be immutable, as you have to keep your Array consistent with the adding/deletion. 此外,如果您使用NSArray作为数据源,并且您将添加/删除行,那么它必须是不可变的,因为您必须保持您的Array与添加/删除一致。 Therefore you need to use NSMutableArray . 因此,您需要使用NSMutableArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM