【问题标题】:iPhone - Static UITableView with >1 sections using Interface BuilderiPhone - 使用 Interface Builder 的具有 >1 个部分的静态 UITableView
【发布时间】:2013-11-24 14:30:50
【问题描述】:

我在我的应用程序中添加了一个新的 TableView。我将单元格类型更改为静态并将一些标签拖到单元格中。现在我想以编程方式访问单元格。例如:第 3 节中的单元格 4 应该打开 google.com 的 safari。

我创建了一个新的 UITableViewController 类。然后我将节数更改为 3,并在 numberofcellsinsection 方法中添加了 switch/case 语句。

如果我运行应用程序并打开表格视图,应用程序就会崩溃。有人可以帮助我设置 TableView 吗?

谢谢!

编辑

这是日志

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:

【问题讨论】:

  • 请发布崩溃日志。
  • @XCodeMonkey 编辑了它
  • 要分别访问不同的单元格,您可能需要为此使用不同的标识符。尝试在单元格中使用 if 或 switch 语句来访问不同的单元格。查看此链接以更好地了解它是如何完成的...stackoverflow.com/questions/14303832/…
  • 我没有任何标识符,因为我使用的是静态单元格。我是否必须更改 cellForRowAtIndexPath 方法中的某些内容?
  • 当您使用静态单元格时,您不应该实现任何数据源方法。您应该阅读“iOS 表视图编程指南”中的“使用数据填充静态表视图”部分。

标签: ios iphone objective-c uitableview


【解决方案1】:

我找到了一个可行的解决方案。

我像这样编辑了 cellForRowAtIndexPath 方法:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell;
}

现在我可以使用 if 语句访问我的静态单元格,例如:

    -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 2){

    if (indexPath.row == 1){

            NSLog(@"Tap");

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"]];       
          }
    }        
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多