ios - 除了 UITableViewController 之外,还有什么方法可以在 UIViewController 中使用原型(prototype)单元吗?

标签 ios xcode storyboard xib

我将我的 tableView 作为 UIViewController 的 subview ,并且在 Storyboard中,我将原型(prototype)单元添加到这个场景。但似乎 Storyboard中单元格的设计不起作用,原型(prototype)单元格似乎只适用于 UITableViewController?

这是我的 Storyboard设计,我使用 tableView 作为 ViewController 的 subview ,并在其中添加两个原型(prototype)单元格(粉色和蓝色单元格) enter image description here

这是我的 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 只会有两个空白单元格(似乎 Storyboard 中的设计不起作用) enter image description here

最佳答案

如果你用 Storyboard 设计,你不需要下面几行

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

相反,您需要为 Storyboard中的单元格设置标识符,如下图所示

enter image description here

关于ios - 除了 UITableViewController 之外,还有什么方法可以在 UIViewController 中使用原型(prototype)单元吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322111/

相关文章:

iphone - 将 UISearchBar 与 NSAttributedStrings 结合使用以增强搜索文本

iphone - UILabel + touchDown

ios - Xcode 10.2 iOS Swift (+ React Native) 项目编译错误

ios - 框架 : Why is iPhone's 4 frame discontinued?

ios - 带有 Segues 到多个 View 的 TableView

ios - XCode 7 UITest 导出日志

iphone - UIImageView 如何跟踪顺时针或逆时针用户运动

objective-c - 我的 "viewDidLoad"在循环中

ios - Xcode 6,Interface Builder 大小问题

iOS 从最后一个 View 的初始 View 导航到第二个 View