![](/img/trans.png)
[英]Resize a TableView of a TableViewController in Storyboard
[英]Prototype tableview cell in a tableviewController in a storyboard
我有一个最初不是故事板应用程序的应用程序。 此后,我为功能分支添加了一个情节提要,并且上面有UITableViewController
的子类。 我创建了带有几个UILabels
和UIImageViews
的原型单元,并为它们每个添加了标签。 原型单元具有正确的标识符。
我已经用标识符注册了课程:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CustomCell"];
当我尝试使自定义单元出队并访问其视图时:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
UIImageView *icon = (UIImageView *)[cell viewWithTag:1];
视图(图标)为nil。
我也尝试过对其进行子类化,并使用重用标识符注册子类,并在原型中使用子类名称设置UITableViewCell
。 在这种情况下,
UIImageView *icon = cell.icon;
仍然返回nil。
情节提要不是主要故事板吗? 我还有其他项目,其中带有自定义subviews
原型单元可以很好地工作而没有所有这些麻烦。 有没有一种方法可以用自定义标识符注册自定义类或UITableViewCell
,但是指定它来自哪个情节提要?
好的,我已经弄清楚了,我将作答,以介绍我所学到的一些小知识。
我的控制器正在使用alloc / init而不是实例化
[_storyboard instantiateViewControllerWithIdentifier:@".."].
这意味着从未使用情节提要,并且从未注册过原型单元。
因此,当使用辅助情节提要板并且以编程方式而不是通过segue实例化控制器时,请确保使用InstantiateViewControllerWithIdentifier。
不要注册单元或注册自定义类:
// don't do this
[self.tableView registerClass:[ClaimsCell class] forCellReuseIdentifier:@"ClaimsCell"];
使用以下步骤使单元出队:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddClaimCell" forIndexPath:indexPath];
这样,编译器实际上会通知您原型单元尚未连接。 不要试图使用旧的tableview出队调用:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddClaimCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
}
因为如果您已正确连接了情节提要,则单元将始终通过forIndexPath:调用返回。
我选择将UITableViewCell与视图标签一起使用,而不是创建自定义类。 但是可以将原型单元设置为自定义UITableViewCell子类,并且如果单个单元元素已连接到情节提要中,则可以引用它们。
实例化UITableViewCell:
UIImageView *icon = (UIImageView *)[cell viewWithTag:1];
UILabel *labelDescription = (UILabel *)[cell viewWithTag:2];
UILabel *labelStatus = (UILabel *)[cell viewWithTag:3];
实例化CustomCell:
UIImageView *icon = cell.iconStatus;
UILabel *labelDescription = cell.labelDescription;
UILabel *labelStatus = cell.labelStatus;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.