虽然设置了委托,但从不调用heightForRowAtIndexPath

JWK*_*Kot 4 objective-c uitableview ios

我已经整天搜索了Google,但我无法找到解决方案.

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

我已经尝试在nib文件和代码中的ShowPostsViewController中设置TableView的委托,但没有任何帮助.我希望问题可能是dataSource已设置但不是委托.我无法理解为什么.

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

ShowPostsViewController.h

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

@end
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

我还在Interface Builder中将tableView链接到ShowPostsViewController. http://s7.directupload.net/images/130525/6e373mth.png

谢谢大家的大力支持.

rma*_*ddy 21

你会为这个错误踢自己.您已实现该方法:

-(CGFloat)tableview: (UITableView*)tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath
Run Code Online (Sandbox Code Playgroud)

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

-(CGFloat)tableView: (UITableView*)tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath
Run Code Online (Sandbox Code Playgroud)

唯一的区别是首都VtableView.你v的错误是小写的.

尝试在Xcode中尽可能多地使用代码完成,以帮助避免这些类型的错误.

  • 如果你的行高是常量(1000),你可以使用rowHeight属性代替委托方法.如果要计算行高,则应在绘制表之前执行此操作. (2认同)