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
275 views
in Technique[技术] by (71.8m points)

ios - UITableView not rendering

So I have a UITableView as a subview of another view in interface builder, and it is not rendering. I've already checked that the number of sections method is returning 1, and that the number of rows in section method is returning a number > 0.

This is my code -

    - (void)viewDidLoad{
    //[super viewDidLoad];
    _trendingImageView.image = [UIImage imageNamed:@"trending.png"];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setClipsToBounds:YES];
    self.scrollView.delegate = self;

    UINib *nib = [UINib nibWithNibName:@"RecommendationCell"
                                bundle:nil];
    [self.recommendationsTableView registerNib:nib forCellReuseIdentifier: reco

    recommendationCellIdentifier];
        self.recommendationsTableView.scrollEnabled = YES;
    }


-(void)executeRecSearch{
    @try{
        [AJAXUtils getAsyncJsonWithUrl:(NSURL *)[NSURL URLWithString:[NSString stringWithFormat:someUrl]] callback:^(NSDictionary *returnjson){
            if(returnjson != nil){
                self.recommendations = returnjson[@"Node"][@"Recommendations"][@"Components"];
                NSLog(@"COUNT: %d", [self.recommendations count]);
                [self performSelectorOnMainThread:@selector(renderRecommendations) withObject:nil waitUntilDone:YES];
            }

        }];

    } @catch (NSException* e) {

    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections{
    return [self.recommendations count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

-(void)renderRecommendations{
    self.recommendationsTableView.hidden = FALSE;
    [self.recommendationsTableView reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"CELL INDEX PATH CALLED: %@", self.recommendations);
    RecommendationCell *cell = [self.recommendationsTableView dequeueReusableCellWithIdentifier:recommendationCellIdentifier];

    if(self.recommendations != nil){
        NSDictionary* recommendationData = self.recommendations[indexPath.row];
    }
    return cell;
}

I also checked that the delegate and dataSource were linked to the File's Owner in the connections manager.

Why is the draw method not being called?

This is my .h file

@interface BasicCardViewController : UIViewController<RateViewDelegate, UIScrollViewDelegate, UITableViewDelegate,UITableViewDataSource>

@property(atomic)NSInteger rating;

@property(copy,nonatomic)NSString *userPageLink;
@property(copy,nonatomic)NSString *nodePage;
@property(strong, nonatomic)NSString* description;
@property(strong,nonatomic)NSString* pageScore;
@property(strong,nonatomic)NSString* pageTitle;
@property(strong,nonatomic)NSString* phoneNumber;
@property(strong,nonatomic)NSString* address;
@property(weak,nonatomic)NSArray* recommendations;
@property(atomic)NSInteger* numberOfRecommendations;


@property (strong, nonatomic) IBOutlet TooviaRateView *rateView;
@property(strong,nonatomic)IBOutlet UIImageView *restaurantImageView;
@property(strong,nonatomic)UIImage* restaurantImage;
@property(strong,nonatomic)IBOutlet UITextView* descriptionView;
@property(strong,nonatomic)IBOutlet UIImageView* trendingImageView;
@property(strong,nonatomic)IBOutlet UILabel* pageScoreLabel;
@property(strong,nonatomic)IBOutlet UILabel* pageTitleLabel;
@property(strong,nonatomic)IBOutlet UIScrollView* scrollView;
@property(strong,nonatomic)IBOutlet UILabel* phoneNumberLabel;
@property(strong,nonatomic)IBOutlet UILabel* addressLabel;
@property(strong,nonatomic)IBOutlet UITableView* recommendationsTableView;

- (IBAction)logOut:(id)sender;
- (IBAction)writeRecommendation:(id)sender;
- (IBAction)call:(id)sender;
-(IBAction)map:(id)sender;

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections;
@end

This is what I get when I print out data source and delegate-

2013-12-17 17:58:41.473 ReviewApp[17451:a0b] DATA SOURCE : *nil description*
2013-12-17 17:58:41.473 ReviewApp[17451:a0b] Data Delegate : *nil description*
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You have to create the cell in

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

dequeueReusableCellWithIdentifier will return nil if no cell is available to dequeue.

Modify it to something similar to this:

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"CELL INDEX PATH CALLED: %@", self.recommendations);
    RecommendationCell *cell = [self.recommendationsTableView dequeueReusableCellWithIdentifier:recommendationCellIdentifier];

    if(cell==nil){
      cell = [[UITableViewCell alloc]init];
    }

    if(self.recommendations != nil){
        NSDictionary* recommendationData = self.recommendations[indexPath.row];
    }
    return cell;
}

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

...