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.
I had a similar problem, my solution was because I did not set the number of sections to 1.
Try changing your
UIView
toUIViewController
and inviewDidLoad
, 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
'sallowsSelection
property tofalse
.Storyboard solution
Select your table view and set the following...
Swift solution
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'snumberOfSections(in tableView:)
method (as other answers suggested). It wasn't. According UIKit's documentation,try this:
i had this stupid problem too
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.
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.