UITableViewCell does not load subviews from storyboard

I think something strange is happening here. I have a cell prototype with multiple labels as subzones and using viewWithTag to try to find them to set their text. I did this 3 more times in one project, so I got lost in what might 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 set a breakpoint, racerNameLabel will be zero, and the cell will not have any subzones:

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

Here, a fragment of the storyboard itself should be defined, showing the signature of the signature. It also shows that the cell id 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 that I set the tag as a dumb number in my frustration, trying to make it work)

+6
source share
1 answer

If you register your custom UITableViewCell cell with the indexer introduced in the Inspector in Interface Builder, this can be a problem. I had the same problem, and when I deleted the lines that registered the user class in viewDidLoad, it worked.

+17
source

All Articles