4

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

我正在尝试在我的 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。 http://s7.directupload.net/images/130525/6e373mth.png

感谢大家的大力支持。

4

1 回答 1

22

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

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

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

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

唯一的区别VtableView. 您错误地使用了小写字母v

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

于 2013-05-25T18:19:05.063 回答