Why the Cellforrowatindexpath method for IOS UITableView is not implemented

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.