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
try this:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
i had this stupid problem too
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.
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
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
UITableViewDelegate
issue (as other answers suggested). It wasn't. I linked it to my view controller in my storyboard. 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
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;
}
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.