简体   繁体   中英

UITableView won't show data

I'm pretty lost right now and have no idea where the problem could be.

I created a single-view application (with ARC & Storyboard), added a tableview to the mainview, set the delegate and datasource of it to my viewcontroller and implemented

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.games count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = [self.games objectAtIndex:[indexPath row]];

    return cell;
}

as protocol methods.

self.games is an array with some NSStrings. When I build and run the application, an empty TableView shows up...

What am I doing wrong?

Are you setting the delegate source & the data source for your tableView?

You need to conform your class to the UITableViewDelegate and UITableViewDataSource protocols. ( cellForRowAtIndexPath: is in the UITableViewDataSource protocol )

Do this by using angle brackets in your class interface definition ie in your .h file:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}

then in your .m file you need to set the delegate file owner of the tableView and also the dataSource. this is done like so -

[tableView setDelegate:self];
[tableView setDataSource:self];

Give this a shot.

Did you try making the games property strong instead of weak? I'm not familiar with ARC, but it seems like your property might be set to nil before you use it.

I think you are missing this code.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM