UITableView not rendering

南笙酒味 提交于 2019-12-14 03:36:14

问题


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*

回答1:


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;
}



回答2:


I had the same problem, none of the provided examples worked, so I rolled out a a simple delay function.

class func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
    }
}}

func renderCategories () {

    Utils.delay(1) {
        self.tableView.reloadData()
    }
}



回答3:


After a ton of stuff - this worked -

I checked use autolayout for the table view(it had been unchecked before, I guess because this view exists inside of a scroll view, and I had unchecked autolayout for that). It then drew - I have no idea why.



来源:https://stackoverflow.com/questions/20630487/uitableview-not-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!