【问题标题】:Content in UITableViewCell is not showingUITableViewCell 中的内容未显示
【发布时间】:2013-11-14 09:07:04
【问题描述】:

我在使用 UITableViewCell 时遇到问题。

我的故事板中有一个视图控制器,它连接到我的视图控制器,名为MainViewController。它包含一个带有 3 个标签的 UITableViewCellUITableViewCell 连接到类 MTTableViewCell

// MTTableViewCell.h

#import <UIKit/UIKit.h>

@interface MTTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
@property (strong, nonatomic) IBOutlet UILabel *statusLabel;

@end

// MTTableViewCell.m

#import "MTTableViewCell.h"

@implementation MTTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

// MainViewController.m

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;        
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }        

    NSString *namecell = @"namecell";
    NSString *statuscell = @"statuscell";           
    [cell.mainLabel setText:namecell];
    [cell.statusLabel setText:statuscell];        

    return cell;
}

问题是当我运行MainViewController 时没有显示任何内容。我错过了什么?我没有得到任何错误,只是一个带有 1 条空白记录的空表视图。

【问题讨论】:

  • 您是否正确设置了网点?
  • 确保调用了 cellForRowAtIndexPath。也许您错过了一些渠道或代表
  • 确保在情节提要中连接表“委托”属性。您还必须将一些数据放入表中。您必须指定每个部分的部分数和行数。
  • 网点设置正确。调用函数 cellForRowAtIndexPath。表 'delegate' 属性当前连接到 MainViewController 文件的所有者
  • 这个关于自定义表格单元的参考比苹果的要好得多。 apeth.com/iOSBook/ch21.html#_custom_cells 另外,如果您对为什么视图和子视图层次结构未在 UITableView 或 UICollection 视图中呈现而感到头疼,那么调试这些问题的一种好方法是从 Xcode 中删除受影响的 .xib 文件,只删除引用,然后添加他们回来了。通常在狙击和切割 IBOutlets 时,即使没有显示错误,笔尖也会因引用损坏而进入不良状态。 Interface Builder 会很高兴地显示并且什么也不说。 Xcode 的 8 大奇迹之一!

标签: ios objective-c uitableview


【解决方案1】:

查看您的界面构建器 - 您应该没有“连接”您的自定义单元格,您应该将其设置为“文件所有者”,因为您实现了视图,不想设置委托。也许这个提示有帮助?

【讨论】:

  • 你是什么意思?所以我不需要将代理插座连接到 MainViewController?
  • 对于您的“MTTableViewCell”,您需要将其设置为自定义类的文件所有者。查看文件检查器中的实用程序区域,并在选择图形单元格时尝试将“自定义类”设置为“MTTableViewCell”。
  • 我解决了。我正在使用界面 MainViewController : UIViewController 而它应该只是界面 MainViewController : UIViewController
  • @Arnout 上面不太可能修复它。原因可能是设计器中的 Restoration ID 属性与可重复使用的单元格标识符不匹配。
【解决方案2】:

您没有从主 Bundle 加载单元格: 使用下面的代码,这对你有用:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"FleetAccountCell";

FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"FleetAccountCell" owner:nil options:nil] objectAtIndex: 0];;
}
    return cell;
}

【讨论】:

    【解决方案3】:

    试试看这里...您的问题的答案可能是

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
    
         CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath];
    
       // your content cell...
    
    
         return cell;
     }
    

    在这里阅读PFQueryTableViewController not showing custom cells

    你好 :)

    【讨论】:

      【解决方案4】:

      这对我有用..

      -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      static NSString *simpleTableIdentifier = @"cellID";
      
      FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
      
      if (cell == nil) {
          cell = [[[NSBundle mainBundle] loadNibNamed:@"Your_nibName" owner:nil options:nil] objectAtIndex: 0];
      }
      [cell layoutIfNeeded];
      return cell;
      }
      

      【讨论】:

        【解决方案5】:

        这样声明TableViewcell

        TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        

        下面一个对我不起作用:

        TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        

        【讨论】:

          【解决方案6】:

          重写你cellForRowAtIndexPath:方法如下

           - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
              {
                  static NSString *CellIdentifier = @"Cell";
          
                  MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                  if (cell == nil) {
                      cell = [[[NSBundle mainBundle] loadNibNamed:@"CUSTOME_CELL_Nib_NAME" owner:nil options:nil] objectAtIndex: 0];;
                      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                  }        
          
                  NSString *namecell = @"namecell";
                  NSString *statuscell = @"statuscell";           
                  [cell.mainLabel setText:namecell];
                  [cell.statusLabel setText:statuscell];        
          
                  return cell;
              }
          

          【讨论】:

          • 自定义单元格在我的故事板中。我认为 nib 名称仅用于 .xib 文件?
          • 对不起@Arnout,我怎么能错过呢?好吧,我认为您可以创建一个用于自定义单元格的笔尖,并在您的故事板中使用它,不是吗?
          • 请检查您的故事板以获取您自定义的单元格标识符,您是否给出了当前的(“单元格”根据您的代码)?
          【解决方案7】:
          -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
              return 4;
          }
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {
              static NSString *cellIdentifier = @"cell";
              TableViewCell *cell = (TableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
              if (cell==nil)
              {
                      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
                      cell = [topLevelObjects objectAtIndex:0];
              }
              return cell;
          }
          - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
          {
              return 1;
          }
          

          【讨论】:

            【解决方案8】:

            我遇到了同样的问题,我在 tableview 委托和数据源中缺少连接:

            self.yourtableviewname.delegate=self;
            self.youtableviewname.datasource=self;
            

            之后一切正常。

            【讨论】:

              【解决方案9】:

              选择单元格并在检查器选项卡中,勾选“已安装”,它将起作用。

              【讨论】:

                【解决方案10】:

                你没有实例化 mainLabel 和 statusLabel,所以它们现在是 nil。你必须在 initWithStyle 方法中实例化它们。

                【讨论】:

                • mainLabel 和 statusLabel 显然已经出现在视图中——他使用插座连接它。所以他不需要隐式初始化它们?
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-09-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多