As we all know, UITableView is a very common UI, but sometimes we run into a uitableviewuitableviewdatasource protocol where Cellforrowatindexpath does not execute, possibly as follows:
1.tableView width or height is equal to 0;
In this case Numberofsectionsintableview and numberofrowsinsection are executed, and Cellforrowatindexpath are not executed.
2. No TableView datasource attributes are set;
This can occur because of:
(1) forgot to write his mistakes;
(2) maintableview.delegate = self; written two times, like this:
Maintableview.delegate = self;
Maintableview.delegate = self;
This is purely a clerical error.
In this case, any method in the Uitableviewdatasource protocol is not executed.
The 3.numberOfSectionsInTableView method returns a value of 0;
In this case Numberofsectionsintableview will execute and Numberofrowsinsection will not be executed.
4.numberOfRowsInSection method returns 0;
In this case Cellforrowatindexpath will not be executed.
Partial test code:
#import "ViewController.h"
@interface Viewcontroller () <UITableViewDelegate,UITableViewDataSource>
{
UITableView *maintableview;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
[Self initui];
}
-(void) Initui
{
The height is set to 132 because the Heightforrowatindexpath method in Uitableviewdelegate is not implemented, and the cell default height is 44,132 = 44 * 3;
Maintableview = [[UITableView alloc]initwithframe:cgrectmake (0, 375, 132) Style:uitableviewstyleplain];
Maintableview.delegate = self;
Maintableview.datasource = self;
[Self.view Addsubview:maintableview];
}
#pragma mark--<UITableViewDelegate,UITableViewDataSource>
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
NSLog (@ "Numberofsectionsintableview");
return 1;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
NSLog (@ "numberofrowsinsection");
return 10;
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
NSLog (@ "Cellforrowatindexpath");
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cellid"];
cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "CellID"];
Cell.textLabel.text = [NSString stringwithformat:@ "%ld", Indexpath.row];
return cell;
}