【问题标题】:How to add static TableView to ViewController如何将静态 TableView 添加到 ViewController
【发布时间】:2013-10-20 10:03:53
【问题描述】:

如何在不使用容器和 UITableViewController 的情况下将静态 TableView 添加到 ViewController?

我认为前段时间是可能的,但现在有了最新的 xCode 故事板和 iOS 7 就不行了。 没有有效答案的类似问题在这里 - iOS 7 change UIView to UITableView 我的表有一点不同——我尝试添加一个静态数据。 我在程序启动时遇到应用程序崩溃。 如果我只设置数据源,程序就会崩溃。如果我取消链接数据源并保留委托和出口,则程序将在没有警告但有空单元格的情况下启动。 备注 - 在没有我帮助的情况下,Storyboard 在视图树中将 ViewController 的命名从 ViewController 更改为 TableViewController。我认为它发生在添加 IBOutlet UITableView *myTableView 之后。为什么会这样?这是什么意思?

【问题讨论】:

  • 您有任何有关崩溃的消息吗?
  • 2013-10-20 13:20:58.154 test[9133:a0b] -[MYTableViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x8d33300 2013-10-20 13:20:58.160 测试[9133:a0b] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MYTableViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x8d33300”

标签: ios uitableview uiviewcontroller


【解决方案1】:

在 UITableViewDataSource 中有 2 个必需的方法:

– tableView:cellForRowAtIndexPath:  required method
– tableView:numberOfRowsInSection:  required method

这意味着在 MYTableViewController 中您必须实现这样的功能(例如 3 个单元格 Test1、Test2 和 Test3):

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyStaticCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Test%d",indexPath.row+1];
    return cell;
}

【讨论】:

  • 编译错误 - 用这个字符串赋值给只读属性:cell.textLabel = [NSString stringWithFormat:@"Test%d",indexPath.row+1];顺便说一句,这意味着我不能使用 Storyboard 来绘制带有子标签、样式等的单元格,并且必须以编程方式完成所有操作。我说的对吗?
  • textLabel.text(我更新了代码)。这种方式是以编程方式进行的,但您可以使用 Storyboard 来完成!
  • 如何使用 Storyboard?
  • 我试过了: UITableView *tbv = [self.storyboard instantiateViewControllerWithIdentifier:@"myTable01"];单元格 = [tbv dequeueReusableCellWithIdentifier:@"Cell01"];但它崩溃了......
  • 我在情节提要中添加了新的 TableViewController,添加了 TableView 和静态单元格,设置了颜色和标签。然后我试图访问它: UITableViewController *tbv = [self.storyboard instantiateViewControllerWithIdentifier:@"myTable01"]; [tbv视图]; UITableViewCell *cell = [tbv.tableView dequeueReusableCellWithIdentifier:@"Cell01"];返回单元格;但是细胞=零!请帮助访问使用情节提要绘制的单元格...
猜你喜欢
  • 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
相关资源
最近更新 更多