简体   繁体   中英

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

-(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.

(id)dequeueReusableCellWithIdentifier:(NSString *)identifier

Return Value

A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.

So I'm guessing that method returns nil for you :) Try a version that takes a indexPath :)

I ran into this problem in 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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