简体   繁体   中英

UITableView delegate and dataSource methods not getting called

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.

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.

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

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.

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.

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

try this:

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

i had this stupid problem too

Try changing your UIView to 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.

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

Storyboard solution

Select your table view and set the following... 在此输入图像描述

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 :]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM