[英]Empty prototype cell for a UITableView
我有一个新项目,必须将原型单元拖到现有的表格视图中。 然后我
然后在我的tableview委托中,当我得到委托回调时:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:11];
我看到nameLabel为零。 我已经仔细检查过标签,并且三重检查了标签和可重复使用的标识符,但是没有运气。 在情节提要中,我看到tableview具有pro type单元格作为其单元格,并且contentView显示了我的标签。 我想念什么?
您是否曾经在if (cell == nil)
分支中创建一个新单元if (cell == nil)
? 如果是,则在此处创建常规的UITAbleViewCell。 不要期望在那里有任何自定义标签,因为您不会从任何nib文件或情节提要中加载它。
单元格对象是什么类型的? NSLog或在调试器中查看实际创建的类型。
您正在分配不包含自定义标签的UITableViewCell。 如果使用Storyboard创建了一个View,则应使用以下方法分配该View。
您可以创建一个Utility方法来从NibName获取Class实例。
+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass
{
if (nibName && objClass)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
for (id currentObject in objects )
{
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}
在您的代码中像这样使用
CustomViewCell *cell = (CustomViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomViewCell"];
if (cell == nil)
{
cell = [Utility loadNibNamed:@"CustomViewCell" ofClass:[CustomViewCell class]];
}
cell.yourLabel.text = @"Dummy Text";
希望这会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.