HeightForRowAtIndexPath is never called even though a delegate is set

Iโ€™ve been searching Google all day, but I just canโ€™t find a solution for this.

I am trying to implement a table view with custom cells in my iOS application. I use custom cells displaying different images and labels. Everything is working fine, except that the cells are too large to represent the table. I know that I need to implement the heightForRowAtIndexPath method, but it is never called.

I tried setting the delegate for TableView in the nib file and in ShowPostsViewController in the code, but nothing helps. I expect the problem is probably that dataSource is installed but not a delegate. However, I do not understand why.

Every solution that I have found so far suggests that the delegate is not installed correctly. However, I am sure that this is in my case ?! I am grateful for any help. Here is 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 also have a tableView associated with ShowPostsViewController in Interface Builder. http://s7.directupload.net/images/130525/6e373mth.png

Thank you all for your great support.

+4
source share
1 answer

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

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

But the actual delegation method should be:

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

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

Try to use as much code in Xcode as possible to avoid such errors.

+21
source

Source: https://habr.com/ru/post/1482744/


All Articles