Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView - cellForRowAtIndexPath Is Not Being Called [duplicate]

I know the title is illustrating enough, and I KNOW that this question has been ask a couple of times, but in this case I couldn't manage to make it work.

I already have worked with UITableViews and they all have been working fine, but this time, I even checked my code with other working copies, but it is not working.

So, here's the story :

I have a table view in one of my view controller's in storyboard with a custom class named ContactViewController, and it is connected to code using an outlet named favContactsTable

Here are related parts from its .h and .m :

ContactViewController.h

@interface ContactViewController : UIViewController <UITableViewDataSource>

// Other Stuff
   .
   .

@property (retain, nonatomic) IBOutlet UITableView *favContactsTable;

   .
   .
// Other Stuff

@end

And here is implemented functions of UITableViewDataSource protocol :

ContactViewController.m

// Rest of the code
.
.
.
.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"TEST OUTPUT");

    //Test
    int a = 1; // Breakpoint
    a++;

    cell = [self.favContactsTable dequeueReusableCellWithIdentifier:@"FavContactCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FavContactCell"];
    }
    cell.textLabel.text = @"FAV";
    .
    .
    .
}

I have another button in my view controller that reloads data for this table, but when I press it none of the breakpoints is triggered and no log is printed, none of the functions are being called. Here's the code for that button :

- (IBAction)refreshTables:(id)sender
{
    [self.favContactsTable reloadData];
}

I checked almost every part with my other projects that has a dynamic table view like this, and I can't figure out what's wrong.

Please do not mention other similar questions as duplicates, because I already have read them all ( or at least, most of them ) and I'm sure I'm not returning 0 rows for each section, and I'm sure favContactsTable is not nil when I send reloadData message to it and ... etc !

Any idea that may help is really appreciated.

like image 576
Sepehrom Avatar asked Feb 15 '14 06:02

Sepehrom


2 Answers

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {


}

set delegate

enter image description here

drag to your viewcontroller icon

enter image description here

check your cell identifier as same in your code also

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"SimpleTableCell"];

    }
return cell;

}
like image 85
codercat Avatar answered Oct 03 '22 21:10

codercat


try this code :

    @interface ContactViewController : UIViewController <UITableViewDataSource, UITableViewDelegate >
.
.
.

and then in viewDidLoad add delegate to your tableview property example :

self.favContactsTable.delegate = self;
like image 39
Robert Avatar answered Oct 03 '22 21:10

Robert