简体   繁体   English

用于HeaderView的UITableViewCell无法从情节提要中加载子视图

[英]UITableViewCell for headerview not loading subviews from storyboard

I have used a simple prototype cell from storyboard for adding header view to my table view. 我使用情节提要中的一个简单原型单元将标头视图添加到我的表视图中。 I added some labels in cell and give specific tag to every label, but while accessing it returns me nil value for [cell viewWithTag :] method.(I have not created a custom class for cell) I am using following method 我在单元格中添加了一些标签,并为每个标签赋予了特定的标签,但是在访问它时,我为[cell viewWithTag:]方法返回了零值。(我尚未为单元格创建自定义类)我正在使用以下方法

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewCell * headerView = [tableView dequeueReusableCellWithIdentifier:@"HeaderView"];

    UILabel * lblVenueName = (UILabel *)[headerView viewWithTag:100];
    lblVenueName.text = @"Test Venue";

    return headerView;
}

for above code the value for lblVenueName always return nil. 对于上面的代码,lblVenueName的值始终返回nil。

(id)dequeueReusableCellWithIdentifier:(NSString *)identifier (id)dequeueReusableCellWithIdentifier:(NSString *)标识符

Return Value 返回值

A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue. 具有关联标识符的UITableViewCell对象;如果在可重用单元格队列中不存在这样的对象,则为nil。

So I'm guessing that method returns nil for you :) Try a version that takes a indexPath :) 所以我猜该方法为您返回nil :)尝试使用采用indexPath的版本:)

I ran into this problem in XCode 7.0 beta 5 (7A176x). 我在XCode 7.0 beta 5(7A176x)中遇到了这个问题。 I suppose it's a bug. 我想这是一个错误。 I've checked in debugger and turns out that cell contentView does not have any subviews at runtime. 我已经检查了调试器,结果发现单元格contentView在运行时没有任何子视图。 If you are designing for some specific screen size then this workaround may help you. 如果您要设计一些特定的屏幕尺寸,则此解决方法可能会对您有所帮助。 Switch to the base values like this: 像这样切换到基本值: 屏幕尺寸检查屏幕截图

Then in attributes inspector check that both "installed" options are checked on all views that you need and their superviews (for example for all views in prototype cell, table view and it's superview): 然后在属性检查器中,检查是否在您需要的所有视图及其超级视图上都选中了“已安装”选项(例如,对于原型单元格,表视图及其超级视图中的所有视图):

“已安装”选项的屏幕截图

After that you can return to your specific screen size and viewWithTag() will return correct values. 之后,您可以返回到特定的屏幕尺寸,并且viewWithTag()将返回正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM