【问题标题】:Talking to UITableViewCell and UITableView from ViewController从 ViewController 与 UITableViewCell 和 UITableView 对话
【发布时间】:2011-10-26 20:13:42
【问题描述】:

我在我的故事板视图控制器中设置了一个带有自定义单元格的表格视图,并希望从我的 ViewController 类中控制它。我已经设置 ViewController 作为我的代表。当我运行该应用程序时,它只会显示一个空列表,并且不会调用我的一个带有标签的单元格。我希望在列表中看到这个标签。我已将数据源和委托连接到情节提要中的 ViewController。我的 .h 和 .m 文件如下,为简单起见进行了编辑。

当我运行它时,我得到了'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 所以看起来委托工作不正常。我错过了什么?我需要将电池连接到任何东西吗?

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end



@implementation ViewController
...

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

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

@end

【问题讨论】:

  • @Srikar。在发布之前,我已尽可能彻底地研究了这一点。那为什么要记下来呢?让我们的菜鸟休息一下。
  • 为什么假设我把它标记下来了?没做,伙计...我只是编辑了你的问题,让它读起来更好。

标签: iphone cocoa-touch uitableview ios5


【解决方案1】:

您对[tableView dequeueReusableCellWithIdentifier:] 的调用返回零。这可能意味着您尚未在 Interface Builder 中设置自定义单元格的标识符(应该是“StateCell”)。当您在 IB 中选择自定义单元格时,您可以在属性检查器中找到此设置。

【讨论】:

  • 搞定了,谢谢罗宾。我的方法中有 ReportItem,属性检查器中有 ReportCell。
【解决方案2】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *CellIdentifier = @"StateCell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
     if (cell == nil){
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                           reuseIdentifier:CellIdentifier] autorelease]
     }
   return cell;
 }

【讨论】:

  • 谢谢 aahsanali。它仍然不起作用,现在只分配一个空单元格。这表明我的原始电话返回 nil 而不是单元格。为什么我不知道?
  • 您能告诉我您是如何定义/制作自定义单元格的吗?
  • 我发现了这个问题。只是那个堆栈不会让我在发布后 8 小时内回答。这是一个简单的错字。我在情节提要中输入了@“ReportItem”,我将重用单元格标记为 ReportCell!呵呵
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多