UITableView delegate and dataSource methods not ge

2020-07-03 01:37发布

I have a UIView which contains a UITableView. The tableview's delegate is set to my UIView, but it never calls the delegate methods:

-(id)init {
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.layer.cornerRadius = PanelCornerRadius;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"height for row");
    int height = [self heightForRowAtIndexPath:indexPath];

    return height;
}

#pragma mark - UITableViewDataSource methods

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

    NSLog(@"number of rows");
    if (self.manager.fetchOperation.dataFetchProblem) {
        return 1;
    }

    int numberOfRows = [self.delegate numberOfSectionsInPanel:self];

    return numberOfRows;
}

I've explored every option I can think of, but can't seem to find the root of the problem.

EDIT: Included the numberOfRowsInSection. It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called.

8条回答
Evening l夕情丶
2楼-- · 2020-07-03 02:14

I ran into the same issue once, what silly mistake I did was inside initWithCoder I called [super init]. TableView was in xib

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init]
}

Instead of

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
}

Just check if that's not the case

查看更多
Bombasti
3楼-- · 2020-07-03 02:15

Can you please write the code that you have written on your cellForRowAtIndexPath: method? Because i can't find anything wrong with your code.

Are you calling your UIView from some other ViewController? If yes, how?

I have done something like this once. I declared a method in the UIView like this :

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}

And then i called the setUpTableView from the view controller i added this view to, like this :

[self.yourView setUpTableView]; 

This might help. Thanks.

查看更多
登录 后发表回答