ViewWithTag:首次在UITableViewCell上返回nil

时间:2014-10-13 15:56:18

标签: ios objective-c uitableview

首次从故事板中通过 dequeueReusableCellWithIdentifier加载UITableViewCell: viewWithTag:返回nil对象。但是在第二次和下次重新加载时,它会返回正确的对象。

我的 tableView 嵌入了原型单元格的UIViewController(在Storyboard中)

cellAttachment = [_tableView dequeueReusableCellWithIdentifier:@"cellAttachment"];
UIButton *btnAttachment = nil;
btnAttachment = (UIButton*)[cellAttachment viewWithTag:10];

首次加载 cellAttachment 时, btnAttachment 为零。 在下次重新加载 btnAttachment 时,会正确返回。

首先加载单元格视图层次结构

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

下次重新加载时的单元格视图层次结构

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

1 个答案:

答案 0 :(得分:20)

在遇到问题后,我将旧的工作文件与新的故事板文件进行比较,以了解可能导致此类行为的原因。在这之后,我能够通过以下方式之一解决我的问题。

1:在UITableViewCell的每个子视图上启用“已安装”字段

enter image description here

2:在.storyboard文件上禁用“使用尺寸类”

enter image description here