简体   繁体   中英

UITableViewCell not loading subviews from storyboard

I think something odd is happening here. I've got a prototype cell with a few labels as subviews, and using viewWithTag to try and find them to set their text. I've done it 3 other times in the same project so I'm lost at what could be wrong.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Result Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UILabel *racerNameLabel = (UILabel*) [cell viewWithTag:5002153];
    //more labels ... 
    return cell;
}

If I put in a breakpoint, racerNameLabel is nil and the cell has no subviews:

(lldb) po [[cell contentView] subviews]
    (id) $5 = 0x08161da0 <__NSArrayI 0x8161da0>(

)

Here's a snippit of the Storyboard itself, showing the label subview should definitely be there. It also shows the cell identifier is correct.

<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Result Cell" rowHeight="80" id="hGh-MB-iBH">
        <rect key="frame" x="0.0" y="22" width="382" height="80"/>
        <autoresizingMask key="autoresizingMask"/>
        <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
            <rect key="frame" x="0.0" y="0.0" width="382" height="79"/>
            <autoresizingMask key="autoresizingMask"/>
            <subviews>
                <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" tag="5002153" contentMode="left" text="Racer Name" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sf6-ol-OLA">
                    <constraints>
                        <constraint firstAttribute="width" constant="166" id="iI1-0U-gN8"/>
                    </constraints>
                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                    <color key="highlightedColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                </label>

(note I set the tag as a stupid number in my frustration trying to get it working)

If you are registering your custom UITableViewCell cell with the indetifier you entered in Inspector in Interface Builder, that might be the problem. I had the same issue and when I removed the lines that were registering the custom class in viewDidLoad, it worked.

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