当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - cellForRowAtIndexPath not called but numberOfRowsInSection called

问题描述:

I have a simple problem but I can't understand what is.

I have a UIViewControler (called RootController) that load a UIView (called SecondView) that contain a tableView. The problem is that the UIView call the numberOfSectionsInTableView and the numberOfRowsInSection but don't call cellForRowAtIndexPath and the table view is not displayed.

The code of the RootViewController is:

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];

[self.view addSubview:secondView];

And the code of the SecondView is:

@interface SecondView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,retain) UITableView *table;

@end

@implementation SecondView

@synthesize table;

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

self.table = [[UITableView alloc] init];

self.table.delegate = self;

self.table.dataSource = self;

[self addSubview:self.table];

}

return self;

}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

cell.textLabel.text = @"Prova";

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 5;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

Can you help me to find the problem? Thank you.

网友答案:

You need to set the Frame of UITableView

网友答案:

This could also happen if reloadData is called on a different thread. Make sure it is run on the main thread since all UI stuff has to happen on the main thread.

dispatch_async(dispatch_get_main_queue(),{
            self.myTableView.reloadData()
        });
网友答案:

In XCode6 same weird problem was occurring to me when "Add Missing Constraints" is applied on tableView on Storyboard to adjust views.

To resolve this issue on Storyboard first clear constraints:

then apply constraints in following fashion:

网友答案:

You can only call the viewcontroller's view AFTER viewDidLoad is called. You can't interact with self.view in your init method

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
}

In your case, you need to init your tableview with a frame (like a suggested in the code above). Just make sure you add the code in viewDidLoad in your viewController

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60,     self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];
分享给朋友:
您可能感兴趣的文章:
随机阅读: