UIViewController中的UITableView不显示内容

时间:2013-11-25 15:56:10

标签: ios uiviewcontroller uitableview

我在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;
}

我可以遗漏什么?谢谢!

4 个答案:

答案 0 :(得分:1)

对我有用的是点击并将dataSourcedelegate个网点直接拖到ViewController所在的UITableView下面的黄色圆圈。我正在尝试以编程方式设置tableView的委托,但它没有完成任务。

答案 1 :(得分:0)

确保您已在Storyboard中输入了单元格的单元格标识符和类名。

当使用像这样的Storyboard和原型单元格时, dequeueReusableCellWithIdentifier 不应该返回nil。 如果队列中没有单元,它将自动创建新单元格。

答案 2 :(得分:0)

在故事板中,将UITableViewCell类设置为FavoriteCell。删除对allCells的引用

答案 3 :(得分:0)

如果在UIViewController中添加UITableView,则需要将UITableView的帧大小设置为与UIViewController内视图的帧大小相同,否则tableview大小可能为0,无法显示任何内容。

如果您使用以下代码在代码中创建UITableView,则可以设置UITableView的帧大小:

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height - 50;
    CGRect tableFrame = CGRectMake(x, y, width, height);

    UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
    // Set tableview delegate and datasource here
    [self.view addSubview:tableView];
}

如果您在案例中按故事板创建UITableView,则设置帧大小:

- (void)viewDidLoad {
        [super viewDidLoad];
        // Set tableview delegate and datasource here
        faveTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}