Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heightForRowAtIndexPath is never called though delegate is set

I already searched Google the entire day but I just can't figure out a solution for this one.

I am trying to implement a table view with custom cells in my iOS App. I'm using custom cells that display different images and Labels. Everything is working fine except that the Cells are too large for the table view. I know that I need to implement the heightForRowAtIndexPath method but it is never called.

I've tried setting the delegate for the TableView in the nib file and in the ShowPostsViewController in the code but nothing helps. I expect that the problem probably is that the dataSource is set but not the delegate. I can't understand why though.

Every solution I found until now says that the delegate is not set correctly. However, I'm pretty sure it is in my case?! I'm grateful for any help. Here's my code:

ShowPostsViewController.h

@interface ShowPostsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *postsTableView;

@end

And ShowPostsViewController.m

@implementation ShowPostsViewController
@synthesize postsTableView;


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.postsTableView.delegate = self;
    self.postsTableView.dataSource = self;
    NSLog(@"Delegate set");

    [postsTableView beginUpdates];
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for(int i=0; i<8; i++){
        [tempArray addObject: [NSIndexPath indexPathForRow:i inSection:0]];
    }
    [postsTableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationAutomatic];
    NSLog(@"Updates Called");


    [postsTableView endUpdates];
    [postsTableView reloadData];
}

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

//PostTableViewCell is my custom Cell that I want to display
-(PostTableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[PostTableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cell"];
    }
    return cell;
}

//This method is not called for some reason
-(CGFloat)tableview: (UITableView*)tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath{
    NSLog(@"Height Method called");
    CGFloat returnValue = 1000;
    return returnValue;
}

//This method is called
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"Section Number called");
    return 1;
}
@end

I've also got the tableView linked to the ShowPostsViewController in the Interface Builder. http://s7.directupload.net/images/130525/6e373mth.png

Thank you all for your great support.

like image 677
JWKot Avatar asked Nov 30 '22 01:11

JWKot


1 Answers

You are going to kick yourself for this mistake. You have implemented the method:

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

But the actual delegate method should be:

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

The only difference is the capital V in tableView. You have a lowercase v by mistake.

Try to make use of as much code completion in Xcode as you can to help avoid these types of mistakes.

like image 54
rmaddy Avatar answered Dec 09 '22 16:12

rmaddy