我知道之前已经问过这个问题,但我还没有找到这个问题的答案。 我一直跟着苹果教程一步一步,但我无法在桌面视图中看到我的数据。
问题是,虽然numberOfSectionsInTableView返回1并且numberOfRowsInSection也返回,但从未调用过func“cellForRowAtIndexPath”。
这是我的代码:
#import "StudentListTableViewController.h"
#import "Student.h"
@interface StudentListTableViewController ()
@end
@implementation StudentListTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadInitialData];
NSLog(@"yes");
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"NUMBERS OF SECTION IN TABLE");
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"NUMBER OF ROWS");
return self.studentList.count;
}
-(void) loadInitialData
{
NSLog(@"LOAD INITIAL DATA");
self.studentList = [[NSMutableArray alloc] init];
Student* s1 = [[Student alloc] init];
s1.firstName = @"moses";
s1.lastName = @"gonzales";
s1.phoneNumber = @"050-20202020";
s1.studentID = @"90123456";
/* Student* s2 = [[Student alloc] init];
s2.firstName = @"roman";
s2.lastName= @"wolf";
s2.phoneNumber = @"050123456789";
s2.studentID = @"39393939";
*/
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"prototypeCell" forIndexPath:indexPath];
NSLog(@"fcell");
// Configure the cell...
Student* s = [self.studentList objectAtIndex:indexPath.row];
cell.textLabel.text = s.firstName;
return cell;
}
和平:)
答案 0 :(得分:2)
您的self.studentList
是空数组,因此它返回0作为计数。如果UITableView
包含0个部分,则不会调用其cellForRowAtIndexPath:
。尝试更改loadInitialData
方法。(将s1
添加到self.studentList)。