Tôi có một tình huống mà bảng của tôi đang tải trước khi nhận dữ liệu từ dịch vụ web của tôi.UITableView xuất hiện trước khi nhận dữ liệu từ connectionDidFinishLoading
Khi tôi gỡ lỗi, tôi có thể thấy rằng NSMutableArray
của tôi có 123 đối tượng nhưng không được phản ánh trong bảng.
Đây là phương pháp connectionDidFinishLoading
tôi:
-(void)connectionDidFinishLoading:(NSURLConnection *)conn{
myData = [[NSString alloc] initWithData:xData encoding:NSUTF8StringEncoding];
SBJsonParser *jParser = [SBJsonParser new];
NSError *error = nil;
NSArray *jsonObjects = [jParser objectWithString:myData error:&error];
NSMutableArray *books = [[NSMutableArray alloc] init];
for(NSDictionary *dict in jsonObjects)
{
Book *b = [Book new];
b.bookName =[dict objectForKey:@"name"];
b.published = [dict objectForKey:@"published"];
[books addObject:b];
}
self.bookArray = books;
[self.tableView reloadData];
NSLog(@"myData = %@",myData);
}
Nếu tôi gỡ lỗi này tôi nhận được của tôi jsonObjects
và Populating sưu tập của tôi. Nhưng tôi nhận thấy bảng của tôi vẫn còn trống.
Chỉ cần làm rõ đây là phương pháp của tôi tableView
:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
Book *b = (Book *)[self.bookArray objectAtIndex:[indexPath row]];
cell.textLabel.text = b.bookName;
....
bất cứ ai có thể giúp tôi với những gì tôi đang thiếu?
Hãy xem toàn bộ phương thức cellForRow. Bạn đã triển khai rowsForSection chưa? – Dancreek
@Dancreek Tôi đoán bạn có nghĩa là numberOfRowsForSection? Dù sao, đó và [tableview reloadData] là sự nghi ngờ của tôi khi tôi đọc chủ đề. Ông đang gọi reloadData mặc dù, mà loại bỏ sau này. – barley
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) phần { trả lại bookArray.count; } – Matt