ios - 使用 json 时从 TableView 中删除

标签 ios objective-c json uitableview

我创建了一个应用程序,它使用 json 从我的外部数据库中提取数据并填充一个 TableView 。一切正常,但我现在希望能够通过滑动删除按钮来删除 TableView 中的数据项之一。这是我的第二个 View Controller 的代码,它给我一条错误消息。在头文件中,我还设置了一个名为 jsonNSMutableArray

#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/

相关文章:

ios - 如何检查设备是否通过airplay连接?

ios - 实体 (null) 与键 "title"的键值编码不兼容

java - Spring 持久失败,带有 FK 的 JSON,FK null

javascript - 发送序列化数据与 json 数据

javascript - 使用 Javascript (AJAX) 从 JSON 获取数据选择

iphone - 将 NSString 从一个 ViewController 传递到另一个 VC NSMutableArray

ios - iOS 上/tmp/文件夹中的照片什么时候过期

objective-c - 以编程方式创建 cocoa 应用程序的初始窗口(OS X)

ios - 以编程方式向 UIScrollView 添加按钮时如何禁用水平滚动?

ios - 魔法记录崩溃: NSManagedObjectContext Queue EXC_BAD_ACCESS KERN_INVALID_ADDRESS