Objective c 正确更新tableview中的JSON数据
当用户滑动到更新(UIRefreshControl)时,我很难用新结果更新我的tableview。控制器工作正常,但数据从未更新。我试图擦除原始数据变量和保存所有数据的NSArray,但它似乎不起作用。我不熟悉iPhone编程,所以请原谅我这是个愚蠢的问题 如果我设法修复了它,那么删除我已经提取的所有数据不是错误的吗?考虑到我的大多数用户将使用3G/Edge连接,是否有一种简单的方式附加更改。以下是我的TableViewController实现类:Objective c 正确更新tableview中的JSON数据,objective-c,json,Objective C,Json,当用户滑动到更新(UIRefreshControl)时,我很难用新结果更新我的tableview。控制器工作正常,但数据从未更新。我试图擦除原始数据变量和保存所有数据的NSArray,但它似乎不起作用。我不熟悉iPhone编程,所以请原谅我这是个愚蠢的问题 如果我设法修复了它,那么删除我已经提取的所有数据不是错误的吗?考虑到我的大多数用户将使用3G/Edge连接,是否有一种简单的方式附加更改。以下是我的TableViewController实现类: #import "Nyhetsfane.h"
#import "Nyhetsfane.h"
@interface Nyhetsfane ()
@end
@implementation Nyhetsfane
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Kaller på slide to update.
[self updateTable];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(updateTable) forControlEvents:UIControlEventValueChanged];
[refreshControl setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Dra for å oppdatere"]];
self.refreshControl = refreshControl;
}
- (void)updateTable{
// Viser "spinner" for å symbolisere nettverkstrafikk.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// URL til JSON filen
NSURL *newsUrl = [NSURL URLWithString:@"http://localhost:7192/fadderapp/events.json"];
//URL Requestobjekt for å kontrollere tilkobling
NSURLRequest *newsRequest = [NSURLRequest requestWithURL:newsUrl];
[[NSURLConnection alloc]initWithRequest:newsRequest delegate:self];
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[newsRawData setLength:0];
newsRawData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[newsRawData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
newsCases = [NSJSONSerialization JSONObjectWithData:newsRawData options:0 error:0];
[self.tableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Feil" message:@"Det har oppstått en feil ved nedlastingen av data. Dobbeltsjekk at du er koblet til internett" delegate:nil cancelButtonTitle:@"Fortsett" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [newsCases count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [[newsCases objectAtIndex:indexPath.row]objectForKey:@"navn"];
return cell;
}
...
值得一提的是,以下是我的JSON提要:
[
{"navn": "Registration", "tidspunkt": "Monday 15:05", "beskrivelse": "Demo!"},
{"navn": "Party!", "tidspunkt": "Monday 19:30", "beskrivelse": "Demo"}
]
更新表视图的正确方法是调用
[self.tableView reloadData];
它应该自动再次调用其所有数据源方法。请尝试使用此代码
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
newCases = [NSArray array];
newsCases = [NSJSONSerialization JSONObjectWithData:newsRawData options:0 error:0];
[self.tableView reloadData];
}
初始化新案例是否确定更新后返回的数据与旧数据具有不同的值?是否验证调用了
updateTable
?调用了connectiondFinishLoading
?新闻案例是否包含正确的数据?您好!谢谢你的回答!我已经用UIAlertView检查过了。我得到了下面的答案,似乎分配是个问题……这不应该是个问题。只是猜测。[NSJSONSerialization JSONObjectWithData:…
返回一个数组,不需要分配。哇!似乎就是这样!非常感谢你的回答!你知道这个方法有什么问题吗?我仍然不明白为什么在用新方法覆盖数组之前分配数组会有不同。但没关系,我很高兴您的问题已经解决。@MartinR您还可以删除数组中的所有旧对象。无需再次分配。