从未调用过cellForRowAtIndexPath

时间:2014-12-24 15:57:54

标签: ios objective-c xcode

我知道之前已经问过这个问题,但我还没有找到这个问题的答案。 我一直跟着苹果教程一步一步,但我无法在桌面视图中看到我的数据。

问题是,虽然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;
}

和平:)

1 个答案:

答案 0 :(得分:2)

您的self.studentList是空数组,因此它返回0作为计数。如果UITableView包含0个部分,则不会调用其cellForRowAtIndexPath:。尝试更改loadInitialData方法。(将s1添加到self.studentList)。