【发布时间】: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