【问题标题】:UITableView in UIViewController not showing contentUIViewController 中的 UITableView 不显示内容
【发布时间】: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


【解决方案1】:

对我有用的是单击并将dataSourcedelegate 出口直接拖动到ViewController 下的黄色圆圈UITableView 所在的位置。我试图以编程方式设置tableView 的委托,它没有做这项工作。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • cellForRowAtIndexPath 中的一切工作正常。单元格标识符已设置并与情节提要的值匹配。我的数据源中的数据未应用于表格视图的某处
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      如果在UIViewController里面添加UITableView,需要设置UITableView的frame size和UIViewController里面view的frame size一样,否则tableview size可能为0,无法显示。

      如果你在代码中创建 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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多