【发布时间】:2013-11-25 15:56:09
【问题描述】:
我在 UIViewController 中有一个 UITableView。由于其他控件(例如工具栏)也需要在此视图上,因此不能选择表格视图控制器。
在我的视图控制器中,我实现了 UITableViewDataSource 和 UITableViewDelegate。我已经为我的表格视图声明了一个出口。在我的主故事板中,我设置了表视图的数据源并委托给视图控制器。 (我也尝试过以编程方式设置数据源和委托)
我的 cellForRowAtIndexPath 方法确认正确的数据正在加载到单元格中。
但是,当应用程序运行时,我的表格视图是空的。
查看控制器头文件:
@interface FavoritesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *allFavoriteObjects;
@property (weak, nonatomic) IBOutlet UITableView *faveTableView;
-(void)loadData;
@end
查看控制器实现文件:
-(FavoriteCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
allCells = [[NSMutableArray alloc] init];
FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];
NSString *currentHeightReading = [NSString stringWithFormat:@"%.2fft", fave.currentFeetValue];
NSString *currentCFSReading = [NSString stringWithFormat:@"%dcfs", fave.currentCFSValue];
if([fave currentCFSValue] == 0){
currentCFSReading = @"";
}
NSString *dateTimeLastUpdate = [NSString stringWithFormat:@"Last update: %@", [fave datelastUpdate]];
[cell setGaugeID:[fave stationIdentifier]];
[cell.lblMainTitle setText:[fave stationRealName]];
[cell.lblGaugeLastUpdate setText:dateTimeLastUpdate];
[cell.lblGaugeCFS setText:currentCFSReading];
[cell.lblGaugeHeight setText:currentHeightReading];
[allCells addObject:cell];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [allFavoriteObjects count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
我可以遗漏什么?谢谢!
【问题讨论】:
-
你有没有使用静态单元格?:D
-
不,单元格是动态的
-
只是评论
allCells将始终只包含最后一个单元格。我不知道你用它做什么,但可能是个问题。否则,对于您最初的问题,您是否看到 TableView(设置背景颜色)以确保它在这里可能是一个想法 -
-(FavoriteCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 这看起来有点奇怪,尝试用uitableviewcell替换 favoritecell
-
你在哪里定义cellID?另外,您还记得将故事板中单元格的类更改为 FavoriteCell 吗?
标签: ios uiviewcontroller uitableview