2010-09-15 75 views
0

大家好,添加一個新行中一個UITableView

這是我在這裏的第一篇文章,這是我的問題: 我試圖從REST API調用得到一些數據,並在一個UITableView然後顯示。

這正是我在做:

在viewDidLoad中

:1)在這裏我初始化我的東西表中顯示(也就是空的開頭)2)的正裝表(數組與0行)和3)然後發出HTTP異步調用。

完成此操作我用HTTP響應來做我的東西,準備就緒時我在我的表上調用reloadData。這裏奇怪的發生。

numberOfRowsInSelection返回正確的行數 但是 tableView:cellForRowAtIndexPath:indexPath爲indexPath.row始終返回零! 因此沒有新行添加到表中。

- (void)doJSONRequest{ 
    responseData = [[NSMutableData data] retain]; 

    NSString *addr = [[NSString alloc] initWithFormat:@"http://localhost:8080/blabla/v1/items?count=10"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [addr release]; 
} 

- (void)doJSONRequestWithURL:(NSString *)url{ 
    responseData = [[NSMutableData data] retain]; 

    NSString *addr = [[NSString alloc] initWithFormat:url]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [addr release]; 
} 


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    //NSLog(@"[%@] connection:didReceiveResponse %@",[self class], response); 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    //NSLog(@"[%@] connection:didReceiveData %@",[self class], data); 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"[%@]",[NSString stringWithFormat:@"Connection failed: %@", [error description]]); 
    UIAlertView *alert = [[UIAlertView alloc] init]; 
    [alert setTitle:@"NETWORK ERROR!"]; 
    [alert setMessage:@"App will close"]; 
    [alert setDelegate:self]; 
    [alert addButtonWithTitle:@"Close"]; 
    [alert show]; 
    [alert release]; 
    [alert show]; 
    [alert release]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    //NSLog(@"[%@] connectionDidFinishLoading %@",[self class], connection); 
    [connection release]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    [responseData release]; 

    NSDictionary *res = [NSDictionary dictionaryWithDictionary:[responseString JSONValue]]; 

    NSDictionary *tmp = [res valueForKey:@"reviews"]; 
    tmp = [tmp valueForKey:@"reviews"]; 
    NSEnumerator *e = [tmp objectEnumerator]; 
    NSDictionary *tmp_review; 

    int i=0; 
    while (tmp_review = [e nextObject]) { 
     //NSLog(@"tmp_review %@", tmp_review); 
     //NSLog(@"count %d", i++); 

      MyObject r = [... doing my staff...] 
     [reviews addObject: r]; 
     [r release]; 
    }; 
    [reviewListTableView reloadData]; 
    [responseString release]; 

} 

- (void)viewDidLoad { 
//- (void)initUI { 

    //review is the array where I put my data 
    //it is empty at the beginning 
    reviews = [[NSMutableArray alloc] initWithObjects:nil]; 
    [super viewDidLoad]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [reviews count] 
} 
+0

請張貼一些代碼=) – MishieMoo 2010-09-15 16:58:22

+0

我已經添加了一些代碼,希望它會有所幫助! – 0m4r 2010-09-15 17:22:17

+0

您確定您的結果數組已填充?另外,你可以在你設置單元的地方加入代碼嗎?看來這可能是問題所在。 – MishieMoo 2010-09-21 20:18:46

回答

0

聽起來像你可能沒有正確實施numberOfSectionsInTableView:。您應該在tableView:cellForRowAtIndexPath:indexPath中記錄indexPath.section以查看您傳遞的是哪一部分值。您將有多個indexPath.row值爲零,因爲每個節都有一個零行。

+0

indexPath.section始終返回0,因爲我只有一個部分... – 0m4r 2010-09-15 18:11:01

0

在數組中添加新對象和[mytableview reloadData];

0

執行以下數據源返回的段的數目在表視圖中出現。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 

在tableView:cellForRowAtIndexPath:indexPath檢查節號並做你的操作。

注意:如果你不需要部分,你可以刪除它。