Sau khi phân tích dữ liệu JSON trong một lớp dữ liệu, tôi đặt thuộc tính tiêu đề NSArray * của UIViewController trong một phương thức fillArrays của cùng một lớp dữ liệu. Trong phương thức viewDidAppear của UIViewController, tôi gọi reloadData trên UITableView của mình. numberOfSectionsInTableView kích hoạt và trả về 1, sau đó numberOfRowsInSection kích hoạt và trả về một mảng đếm 4 (cho 4 chuỗi trong mảng). Tuy nhiên, kiểm soát không bao giờ được để cellForRowAtIndexPath và tôi đang có thời gian khó hiểu nhất lý do tại sao, đặc biệt là kể từ khi tôi có phần hợp lệ và hàng. Tất cả các ô đều hiển thị.cellForRowAtIndexPath không được gọi; các phần trả về 1 và các hàng trả về 4
Tôi đã thêm giao thức UITableViewDataSource và UITableViewDelegate vào giao diện UIViewController và đặt đại biểu và dữ liệu của UITableView thành self trong viewDidLoad (cũng được xác minh bằng phương thức đếm hàng và phần được gọi).
Tôi tự hỏi nếu nó có một cái gì đó để với tôi reinitializing UIViewController trong Data.m để thiết lập các thuộc tính của nó.
Trong Data.m:
- (void)fillArrays:(NSArray *)jsonObjs {
NSLog(@"fillArrays");
HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
hrvc.headlines = [self getJsonValuesForKey:@"headline" inArrayOfObjects:jsonObjs];
[hrvc viewDidAppear:NO];
}
Trong ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad");
// Table view
headlineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
[headlineTableView setDelegate:self];
[headlineTableView setDataSource:self];
// Temporary
self.headlines = [[NSMutableArray alloc] initWithObjects:@"headline1", @"headline2", @"headline3", @"headline4", nil];
[self.view addSubview:headlineTableView];
self.headlineTableView = headlineTableView;
[headlineTableView release];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"viewdidappear");
NSLog(@"headlines: %@", self.headlines); // Returns an array of 4 headlines
if([self.headlines count] != 0){
[self.headlineTableView reloadData];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionsInTableView: 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection: %d", [self.headlines count]);
return [self.headlines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [[NSString alloc] initWithFormat:@"%@", [self.headlines objectAtIndex:indexPath.row]];
return cell;
}
bạn có thể muốn đăng một số mã – coneybeare