【问题标题】:Is there any way to use prototype cells in UIViewController other than UITableViewController?除了 UITableViewController 之外,还有什么方法可以在 UIViewController 中使用原型单元格?
【发布时间】:2018-01-01 11:46:14
【问题描述】:

我将 tableView 作为 UIViewController 的子视图,在情节提要中,我将原型单元格添加到该场景中。但似乎情节提要中的单元格设计不起作用,原型单元格似乎仅适用于 UITableViewController?

这是我的故事板设计,我使用 tableView 作为 ViewController 的子视图,并在其中添加两个原型单元格(粉色和蓝色单元格)

这是我的 tableView 代码:

@implementation SubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"TableViewCell"];
    [self.tableView registerClass:[ATableViewCell class] forCellReuseIdentifier:@"ATableViewCell"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

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

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

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
        return cell;
    }
    else
    {
        ATableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ATableViewCell" forIndexPath:indexPath];
        return cell;
    }
}

当我运行我的代码时,我希望会有一个蓝色和粉红色的单元格,但实际上,tableView 只会有两个空白单元格(似乎故事板中的设计不起作用)

【问题讨论】:

  • 您遇到的错误是什么。你能添加一些代码。如果您正确设置了源和委托,它应该可以工作。
  • @kapsym thx,我刚刚发布了我的故事板设计和代码,有什么问题吗?
  • 您能描述一下您期望发生的事情,实际发生的事情吗?在这种情况下,“不起作用”是什么意思?
  • [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"TableViewCell"]; [self.tableView registerClass:[ATableViewCell 类] forCellReuseIdentifier:@"ATableViewCell"];在情节提要的原型单元中不需要。

标签: ios xcode storyboard xib


【解决方案1】:

如果您使用情节提要进行设计,则不需要以下几行

[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"TableViewCell"];
[self.tableView registerClass:[ATableViewCell class] forCellReuseIdentifier:@"ATableViewCell"];

您需要在情节提要中为单元格设置标识符,如下图所示

【讨论】:

    【解决方案2】:

    您想检查一些事情以确保您的表格视图已连接并知道要查找 SubViewController 类中的单元格。

    1. 确保 UITableView 的出口设置为您可以在代码中引用的内容。 self.tableView 不会自动连接,除非您使用 UITableViewController 子类。
    2. 确保您的表视图连接了SubViewController 类作为它的数据源和委托。您可以在代码中执行此操作,也可以通过拖入界面生成器来执行此操作。尝试将此添加到您的 viewDidLoad 方法中:self.tableView.dataSource = self;
    3. 您可能希望将单元格设置为在 Interface Builder 的检查器面板中使用字符串标识符。然后,在cellForRowAtIndexPath: 中,使用该标识符将您的单元格出列,而不是按类名注册。

    顺便说一句,您也可以将原型单元格与 UICollectionView 一起使用,但 API 和情节提要设置非常相似,并且适用相同的步骤。

    【讨论】:

    • 感谢@Moshe,似乎问题是我为原型单元格调用了 registerClass,当我删除这些调用时,单元格显示良好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2016-01-03
    • 2021-12-03
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多