大家好,
这是我的第一篇帖子,这是我的问题: 我试图从REST API调用中获取一些数据,然后在UITableView中显示。
这就是我在做的事情:
在viewDidLoad中:1)这里我初始化我的 things 数组以显示在表中(在开头是空的)2)表被加载(有0行)和3)然后发出HTTP异步调用。
完成这个我用HTTP响应做我的东西,准备好后我在我的桌子上调用reloadData。奇怪的发生在这里。
numberOfRowsInSelection返回正确的行数 但 tableView:cellForRowAtIndexPath:indexPath.row的indexPath总是返回零! 所以没有新的行添加到表中。
- (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 :(得分:0)
听起来您可能没有正确实施numberOfSectionsInTableView:
。您应该在indexPath.section
中记录tableView:cellForRowAtIndexPath:indexPath
,以查看您要传递的部分值。您将有多个indexPath.row值为零,因为每个部分都有一个零行。
答案 1 :(得分:0)
在数组中添加新对象和[mytableview reloadData];
答案 2 :(得分:0)
在表格视图中实现以下数据源返回部分的编号。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
在tableView:cellForRowAtIndexPath:indexPath检查部分编号并执行操作。
注意:如果您不需要部分,可以将其删除。