ViewWithTag: returns nil on UITableViewCell only first time

When UITableViewCell is loaded first time from Storyboard via dequeueReusableCellWithIdentifier: viewWithTag: returns nil object. But on second and next reloads it returns proper object.

My tableView is embedded in UIViewController(in Storyboard) with prototype cells

  • How do you select uitableview rows programmatically
  • load more for UITableView in swift
  • cellForRowAtIndexPath memory management
  • Animation with constraints in UITableViewCell launches incorrectly
  • UITableViewCell label displayed with little slide up animation
  • Swift - UITableViewCell Selected Background Color on Multiple Selection
  •         cellAttachment = [_tableView dequeueReusableCellWithIdentifier:@"cellAttachment"];
            UIButton *btnAttachment = nil;
            btnAttachment = (UIButton*)[cellAttachment viewWithTag:10];
    

    When cellAttachment is loaded first time, btnAttachment is nil.
    On next reload btnAttachment is returned properly.

    Cell View hierarchy first load

    (lldb) po [cellAttachment recursiveDescription]
    <UITableViewCell: 0x7fd3e611cc20; frame = (0 0; 0 0); layer = <CALayer: 0x7fd3e611be00>>
       | <UITableViewCellContentView: 0x7fd3e3fc9850; frame = (0 0; 0 0); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x7fd3e611b6a0>; layer = <CALayer: 0x7fd3e611bd00>>
       | <_UITableViewCellSeparatorView: 0x7fd3e611cf20; frame = (0 -1; 15 1); layer = <CALayer: 0x7fd3e611c020>>
    

    Cell view hierarchy on next reloads

        (lldb) po [cellAttachment recursiveDescription]
        <UITableViewCell: 0x7fd3e611cc20; frame = (0 179; 320 42); hidden = YES; autoresize = W; layer = <CALayer: 0x7fd3e611be00>>
           | <UITableViewCellContentView: 0x7fd3e3fc9850; frame = (0 0; 320 42); opaque = NO; gestureRecognizers = <NSArray: 0x7fd3e611b6a0>; layer = <CALayer: 0x7fd3e611bd00>>
           |    | <UIButton: 0x7fd3e611d0f0; frame = (286 6; 15 22); opaque = NO; autoresize = RM+BM; tag = 10; layer = <CALayer: 0x7fd3e611c0b0>>
           |    |    | <UIImageView: 0x7fd3e6015410; frame = (0.5 1; 14 20); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd3e3ccd960>>
           | <_UITableViewCellSeparatorView: 0x7fd3e611cf20; frame = (0 -1; 15 1); layer = <CALayer: 0x7fd3e611c020>>
    
       | <UIView: 0x7fd3e3c4c140; frame = (15 41; 305 1); layer = <CALayer: 0x7fd3e3cecb40>>
    

    Solutions Collect From Internet About “ViewWithTag: returns nil on UITableViewCell only first time”

    After having the issue I compared my old working files with new storyboard files to know what may cause such a behavior. After doing that I was able to fix my problem in one of following ways.

    1: Enable ‘Installed’ field on each subview of UITableViewCell

    enter image description here

    OR

    2: Disable ‘Use size classes’ on .storyboard file

    enter image description here