Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...