我创建了一个应用程序,它使用 json 从我的外部数据库中提取数据并填充一个 TableView 。一切正常,但我现在希望能够通过滑动删除按钮来删除 TableView 中的数据项之一。这是我的第二个 View Controller 的代码,它给我一条错误消息。在头文件中,我还设置了一个名为 json
的 NSMutableArray
。
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) getData:(NSData *) data{
NSError *error;
json = [ NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self.tableView reloadData];
}
-(void) start{
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer *myTimer = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(start) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
[self start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [json count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:@"name"];
cell.detailTextLabel.text = [info objectForKey:@"message"];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[json removeObjectAtIndex:indexPath.row];
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// [tableView reloadData];
}
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
我知道我需要对其进行编码以使用 php 等从实际数据库中删除。但现在我只想将其从 TableView 中删除。这是我收到的错误消息:'NSInternalInconsistencyException',原因:'-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
最佳答案
这一行:
json = [ NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
为您提供了一个不可变数组 (NSArray
),但您的代码使用 json
就像它是一个 NSMutableArray
。
你有两个选择:
1) 将行更改为:
json = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error] mutableCopy];
或
2) 将行更改为:
json = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
关于ios - 使用 json 时从 TableView 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945272/