本文介绍了ViewWithTag:仅第一次在 UITableViewCell 上返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 UITableViewCell 通过 dequeueReusableCellWithIdentifier: 从 Storyboard 第一次加载时: viewWithTag: 返回 nil 对象.但是在第二次和下一次重新加载时,它会返回正确的对象.

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

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

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

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

当第一次加载 cellAttachment 时,btnAttachment 为零.在下次重新加载 btnAttachment 时正确返回.

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

Cell View 层次结构第一次加载

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

推荐答案

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

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:在 UITableViewCell 的每个子视图上启用已安装"字段

2:在 .storyboard 文件中禁用使用尺寸类别"

这篇关于ViewWithTag:仅第一次在 UITableViewCell 上返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:39