Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

ios - - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called

So its really weird... I have a ViewController called ListEventsViewController

in the header file:

@interface ListEventsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *eventsTable;
@property (strong, nonatomic) NSArray *events;

@end

In the Implementation I am calling the pretty much mandatory functions below:

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    cell = [eventsTable dequeueReusableCellWithIdentifier:@"eventCell"];

    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"eventCell"];
    }

    NSString *textLabelTitle = [[events objectAtIndex:indexPath.row] objectForKey:@"name"];
    NSString *textLabelDetail = [[events objectAtIndex:indexPath.row] objectForKey:@"date"];
    //cell.imageView.image = someimage;
    NSLog(@"Text Label Title: %@", textLabelTitle);
    NSLog(@"Text Label Detail: %@", textLabelDetail);
    cell.textLabel.text = textLabelTitle;
    cell.detailTextLabel.text = textLabelDetail;

    return cell;
}

The problem is the function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ is not being called so the cells arent being populated. also the number of cells isnt being allocated either. What could be causing this?

It looks wired up to me heres a picture of the h and IB. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You illustrated in your screenshot that you have linked the table to its IBOutlet, but you haven't linked the table view's delegate or datasource to your controller and therefore your tables delegate methods such as cellForRowAtIndex will not be called. Linking these should fix your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...