UITableView delegate and dataSource methods not getting called

后端 未结 8 1387
野性不改
野性不改 2021-02-13 14:23

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 = [sup         


        
相关标签:
8条回答
  • 2021-02-13 14:48

    try this:

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

    i had this stupid problem too

    0 讨论(0)
  • 2021-02-13 14:55

    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.

    0 讨论(0)
  • 2021-02-13 14:56

    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

    0 讨论(0)
  • 2021-02-13 15:05

    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 :]
    0 讨论(0)
  • 2021-02-13 15:07

    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;
    }
    
    0 讨论(0)
  • 2021-02-13 15:09

    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.

    0 讨论(0)
提交回复
热议问题