0

当用户滑动更新(UIRefreshControl)时,我很难用新结果更新我的表格视图。控制器工作正常,但数据永远不会更新。我试图擦除原始数据变量和保存所有数据的 NSArray,但它似乎不起作用。我是 iPhone 编程的新手,所以请原谅我这是一个愚蠢的问题。

如果我设法修复它,删除我已经提取的所有数据会不会是错误的?考虑到我的大多数用户将使用 3G/Edge 连接,是否有一种简单的方法来追加更改。这是我的 TableViewController 实现类:

#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"}
]
4

2 回答 2

0

尝试使用此代码

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    newCases = [NSArray array];
    newsCases = [NSJSONSerialization JSONObjectWithData:newsRawData options:0 error:0];
    [self.tableView reloadData];
}
于 2013-04-17T13:24:27.750 回答
-2

更新表格视图的正确方法是调用

[self.tableView reloadData]; 

它应该自动再次调用其所有数据源方法。

于 2013-04-17T13:20:35.300 回答