【发布时间】:2015-11-08 16:54:50
【问题描述】:
今天,我遇到了一件很奇怪的事情。
我有一个由 Storyboard 构建的 tableview。
tableview 的类型是 Static 和 Grouped
在其中一个 Cell 中,我有一个带有开关的单元格(在情节提要中设置为 ON)。
我通过 Storyboard 将开关的标签设置为 10000816。
我尝试使用 [self.tableview viewWithTag 10000816] 来获取 tableview 控制器中的开关。失败。
好的。然后我尝试先获取 Cell 并尝试 [cell viewWithTag] 并以某种方式成功。
那我想知道为什么。
所以我只保留以下行未注释。
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
关注
UISwitch* switchButton = [self.tableView viewWithTag: 10000816];
[switchButton setOn:NO animated:YES];
好的,这一次,再次成功。
我评论了 Get-Cell 行,我认为这无关紧要。
然后奇怪的事情发生了。它失败了。我所做的唯一更改只是一条无关紧要的线。
好奇怪。
问题
如果我只是评论 Get-Cell 行,为什么 viewWithTag 会返回 nil?
- (void) switchOff{
//****** the line below is the magic Get-Cell line , if I comment this line , viewWithTag will return nil
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UISwitch* switchButton = [self.tableView viewWithTag: 10000816];
[switchButton setOn:NO animated:YES];
}
[更新]
我的 switchOff 被调用 - (void)viewDidAppear:
- (void)viewDidAppear:(BOOL)animated{
[self switchOff]
}
【问题讨论】:
-
桌子长什么样?也许在您调用
viewWithTag的那一刻,该单元尚未初始化? -
viewWithTag 在 ViewDidAppear 中被调用
-
呈现表格时单元格是否可见?
-
您需要在
cell上调用viewWithTag:,而不是self.tableView。 -
为什么会得到
viewDidAppear中的第一个单元格?
标签: ios objective-c uitableview storyboard