UITableViewController不显示自定义原型单元格

时间:2014-07-28 19:46:08

标签: ios objective-c uitableview

之前我使用过UITableViewController,虽然大多数是使用Nib而不是Storyboard,我似乎无法填充外观或数据。

我的UITableViewController设置如下:

@implementation BFTBackThreadTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
  self = [super initWithStyle:style];
  if (self) {
    // Custom initialization
}
 return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  _dummyUsers = [[NSArray alloc] initWithObjects:@"@JonathanB",@"@NickyV",@"@Dman",@"C-LO-P", nil];

  NSLog(@"%lu", (unsigned long)[_dummyUsers count]);

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  // Return the number of sections.
  return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  // Return the number of rows in the section.
  return [_dummyUsers count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  BFTThreadTableViewCell *cell = (BFTThreadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

// Configure the cell
  cell.userLabel.text = [_dummyUsers objectAtIndex:[indexPath row]];

  return cell;
}

我检查了数组,并且所有4个对象都在那里,所以他们应该可以填充Row。

我的UITableViewCell class.h文件设置如下:

@interface BFTThreadTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *userLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeStamp;
@property (weak, nonatomic) IBOutlet UIButton *numberMessageThread;

@end

所有这些Outlet都连接到Storyboard中的Cell,并在使用助理编辑器时显示连接。 cellIdentifier是正确的,并确保我复制并粘贴,而不是每次都尝试正确键入它。

故事板的设置如下: storyboard

虽然据我所知,我相信这应该有效,但我仍然无法让它工作,它在模拟器上运行如下:

emulator image

非常感谢任何帮助。 提前谢谢!

4 个答案:

答案 0 :(得分:3)

你必须在numberOfSectionsInTableView中返回一个非零数字:或者只是完全删除该方法,默认为1节。

答案 1 :(得分:2)

您在表格视图中的部分数量返回零,因此它没有显示任何部分。使用此:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  // Return the number of sections.
  return 1;
}

答案 2 :(得分:1)

我花了DAYS试图找到解决方案。

我知道我已经迟到了(而且肯定不是Keeasno的原型细胞没有显示的原因)但是如果上面的答案不是这样的话帮助解决我的问题是我使用了错误的"标识符" Interface Builder中的字段。确保在“界面”构建器中您使用了正确的"标识符"字段提供您的可重用标识符,请参见下文:

XCode Interface Builder Sceenshot

辅助功能标识符与原型单元无关 - 它与为需要辅助功能的用户(例如语音辅助功能)提供有用信息有关,请参阅下面的链接以获取更多信息:

Making Your iOS App Accessible

答案 3 :(得分:0)

在我的逻辑中,为了响应tableView:cellForRowAtIndexPath:如果dequeueReusableCellWithIdentifier返回一个nil值,我自己创建一个实例。代码如下所示:

 static NSString *cellIdentifier = @"CellIdentifier";

 MaterialTableViewCell *cell = [tableView
 dequeueReusableCellWithIdentifier: cellIdentifier];

 if (cell == nil)

          cell = [[MaterialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:
 cellIdentifier];
相关问题