我无法删除主视图控制器中的行

时间:2014-04-04 01:32:23

标签: ios

无法删除行,由于未捕获的异常而导致终止应用程序' NSInternalInconsistencyException',原因:' - [__ NSCFArray removeObjectAtIndex:]:发送到不可变对象的mutating方法

我有以下内容:

#import <UIKit/UIKit.h>
@class CSDetailViewController;
@interface CSMasterViewController : UITableViewController
@property (strong, nonatomic) CSDetailViewController *detailViewController;
@property (strong, nonatomic) NSMutableArray *kryeministrat;
@end

以及实施时的以下内容:

#import "CSMasterViewController.h"
#import "CSDetailViewController.h"

@implementation CSMasterViewController    
- (void)awakeFromNib
{
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

NSString *path = [[NSBundle mainBundle]pathForResource:@"KryeministratList" ofType:@"plist"];
NSDictionary *kryeministratInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.kryeministrat = [kryeministratInfo objectForKey:@"kryeministrat"];
self.detailViewController = (CSDetailViewController *)   [[self.splitViewController.viewControllers lastObject] topViewController];
}
#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.kryeministrat count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSDictionary *kryeministrat = self.kryeministrat[indexPath.row];
cell.textLabel.text = kryeministrat[@"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *kryeministrat = self.kryeministrat[indexPath.row];
NSString *urlString = kryeministrat[@"url"];
self.detailViewController.detailItem = urlString;
}
- (void)tableView:(UITableView *)tableView
 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.kryeministrat removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                 withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end

1 个答案:

答案 0 :(得分:0)

从您的例外

开始,您应该查看以下代码:


NSDictionary *kryeministratInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.kryeministrat = [kryeministratInfo objectForKey:@"kryeministrat"];

虽然将kryeministrat属性声明为 NSMutableArray ,但kryeministratInfoNSDictionary。这意味着kryeministratInfo中的值都是不可变的。

所以你可以这样做:


NSDictionary *kryeministratInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.kryeministrat = [NSMutableArray arrayWithArray:[kryeministratInfo objectForKey:@"kryeministrat"]];