【问题标题】:heightForRowAtIndexPath is never called though delegate is set尽管设置了委托,但从不调用 heightForRowAtIndexPath
【发布时间】:2013-05-26 02:19:05
【问题描述】:

我已经在谷歌上搜索了一整天,但我就是想不出这个解决方案。

我正在尝试在我的 iOS 应用程序中使用自定义单元格实现表格视图。我正在使用显示不同图像和标签的自定义单元格。一切正常,除了单元格对于表格视图来说太大了。我知道我需要实现 heightForRowAtIndexPath 方法,但它从未被调用过。

我尝试在 nib 文件和代码中的 ShowPostsViewController 中设置 TableView 的委托,但没有任何帮助。我预计问题可能是设置了数据源但没有设置委托。我不明白为什么。

到目前为止,我发现的每个解决方案都表明委托设置不正确。但是,我很确定这是我的情况?! 我很感激任何帮助。这是我的代码:

ShowPostsViewController.h

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

@end

还有 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

我还将 tableView 链接到 Interface Builder 中的 ShowPostsViewController。

感谢大家的大力支持。

【问题讨论】:

  • 在我的例子中,只有当 tableView 的初始高度大于 0 时才会调用此方法,我必须将高度设置为 1 才能使其工作。不知道会发生什么

标签: ios objective-c uitableview


【解决方案1】:

你会因为这个错误而自责。你已经实现了这个方法:

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

但实际的委托方法应该是:

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

唯一的区别是tableView 中的大写V。你误用了小写的v

尽量在 Xcode 中使用尽可能多的代码完成来帮助避免这些类型的错误。

【讨论】:

  • 如果你的行高是常数(1000),你可以使用rowHeight属性代替委托方法。如果要计算行高,则应在绘制表格之前进行。
  • @stosha 你应该对这个问题发表评论,而不是我的回答。
  • @Stosha:我知道我试过了。感谢您的回答。不过,我想要不同的单元格有不同的高度。
猜你喜欢
  • 2019-10-28
  • 2021-01-18
  • 2017-09-29
  • 1970-01-01
  • 2014-06-17
  • 2016-01-31
  • 2011-09-04
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多