我将我的 tableView 作为 UIViewController 的 subview ,并且在 Storyboard中,我将原型(prototype)单元添加到这个场景。但似乎 Storyboard中单元格的设计不起作用,原型(prototype)单元格似乎只适用于 UITableViewController?
这是我的 Storyboard设计,我使用 tableView 作为 ViewController 的 subview ,并在其中添加两个原型(prototype)单元格(粉色和蓝色单元格)
这是我的 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 中的设计不起作用)
最佳答案
如果你用 Storyboard 设计,你不需要下面几行
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"TableViewCell"];
[self.tableView registerClass:[ATableViewCell class] forCellReuseIdentifier:@"ATableViewCell"];
相反,您需要为 Storyboard中的单元格设置标识符,如下图所示
关于ios - 除了 UITableViewController 之外,还有什么方法可以在 UIViewController 中使用原型(prototype)单元吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322111/