2010-09-15 77 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检查节号并做你的操作。

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