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条回答
Emotional °昔
2楼-- · 2020-07-03 01:59

I had a similar problem, my solution was because I did not set the number of sections to 1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
查看更多
▲ chillily
3楼-- · 2020-07-03 01:59

Try changing your UIViewto UIViewController and in viewDidLoad, call

[[UITableView alloc] initWithFrame:style:] to create your Table View.

Set self as the delegate and data source like you did above, and then add this UITableView as Subview in this Controller.

查看更多
爷的心禁止访问
4楼-- · 2020-07-03 01:59

I accidentally set my tableView's allowsSelection property to false.

Storyboard solution

Select your table view and set the following... enter image description here

Swift solution

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = true
}

Notes

  1. At first, I thought this was a UITableViewDelegate issue (as other answers suggested). It wasn't. I linked it to my view controller in my storyboard.
  2. Next, I thought it was a UITableViewDataSource issue, in that I didn't implement the protocol's numberOfSections(in tableView:) method (as other answers suggested). It wasn't. According UIKit's documentation,

// Default is 1 if not implemented

  1. Finally, I checked the settings on my table view :]
查看更多
啃猪蹄的小仙女
5楼-- · 2020-07-03 02:01

try this:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

i had this stupid problem too

查看更多
一夜七次
6楼-- · 2020-07-03 02:03

You do not need to use a UIViewController, that's just a smokescreen. You also do not need to add to the .h, although you should do that anyway because Xcode will complain otherwise, and you won't get method name auto complete.

I just wrote a test project that embeds a table view in a normal UIView and it works fine. Just make sure your initialization code is being called. I bet you need to move it to awakeFromNib.

查看更多
疯言疯语
7楼-- · 2020-07-03 02:11

You should declare your view like this @interface MyView : UIView <UITableViewDelegate, UITableViewDataSource>

BTW: I would have a ViewController that "controls" your view and your tableview, and have the ViewController be the delegate and datasource of the table view.

查看更多
登录 后发表回答