arunkumar pandian
arunkumar pandian

Reputation: 11

Objective - C - UITableView cellForRowAtIndexPath never called

I know that this question have been asked so many times but unfortunately non of the solutions in those questions worked for me.

Here are some of the things I tried:

making sure that numberOfSectionsInTableView is not returning 0 making sure that numberOfRowsInSection is not returning 0. insuring that reloadData is being called on the main thread by using performSelectorOnMainThread as shown below:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]

setting the delegate :

 self.tableView.dataSource = self; self.tableView.delegate = self;

making sure that tableview background color was changed.

making sure that tableview frame was adjustable.

But cellForRowAtIndexPath method never called

Please find the code:

UITableView *tableView;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView]
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 3;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = "test";
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 44;

}

If you have any idea what to do, please let me know.

Thanks

Upvotes: 1

Views: 169

Answers (4)

Dishant Rajput
Dishant Rajput

Reputation: 1357

First

@interface yourController ()<UITableViewDelegate,UITableViewDataSource>

Second

-(void)viewDidLoad {
[super viewDidLoad];



self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];

[self reloadData]; //call your webservice here
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]
[self.tableView reloadData]; //most imp
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return yourArray.count; //your array which is get from webservice

}

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20369

You created the tableView added it as subView to your ViewController's view in ViewDidLoad() which is fine :) But where are you calling reload Data on tableView buddy :)

Add

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,480) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView]
    [self.tableView reloadData];
}

It will work like a charm :)

Upvotes: 1

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

you have to delegate and datasource Protocol to view controller

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

Upvotes: 0

user6930374
user6930374

Reputation:

just uninstall app from simulator and run again. sometimes its not work.

Upvotes: 0

Related Questions