[英]In iOS , Why will viewWithTag method return nil?
I have a tableview which is built by Storyboard. 我有一个由Storyboard构建的tableview。
the type of the tableview is Static and Grouped 表格视图的类型为“ 静态”和“分组”
In one of its Cell , I have a cell with a switch(which is set ON in Storyboard). 在它的一个Cell中,我有一个带开关的单元(在Storyboard中设置为ON )。
I set the switch's tag to 10000816 by Storyboard. 我通过Storyboard将开关的标签设置为10000816。
I try to use [self.tableview viewWithTag 10000816] to get the switch in the tableview's controller. 我尝试使用[self.tableview viewWithTag 10000816]在tableview的控制器中获取开关。 Failed.
失败了
OK. 好。 I then try to get the Cell first and try [cell viewWithTag] and somehow, it succeed.
然后,我尝试首先获取Cell并尝试[cell viewWithTag],以某种方式,它成功了。
Then I wonder why. 然后我想知道为什么。
So I just left the following line uncommented. 所以我只留下了下面的注释。
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
Following by 跟随者
UISwitch* switchButton = [self.tableView viewWithTag: 10000816];
[switchButton setOn:NO animated:YES];
OK , this time , succeed again. OK,这次,再次成功。
And I comment the Get-Cell line,which I think irrelevant. 我评论了与我无关的Get-Cell系列。
then strange thing happened.It failed. 然后发生了奇怪的事情。失败了。 The only change I had made is just a irrelevant line.
我所做的唯一更改只是一条无关紧要的路线。 So strange.
这么奇怪。
Why will viewWithTag return nil if I just comment the Get-Cell line? 如果我只注释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];
}
[update] [更新]
my switchOff is called in - (void)viewDidAppear: 我的switchOff在-(void)viewDidAppear中被调用:
- (void)viewDidAppear:(BOOL)animated{
[self switchOff]
}
If your tableview is a Prototype one (one whose value will be filled by cellForRowAtIndexPath
method) then dropping a cell inside the tableview, you wont get to see the cell, as that cell is not being created, so is its content. 如果您的表视图是一个原型(其值将由
cellForRowAtIndexPath
方法填充),然后将一个单元格放到表视图中,您将不会看到该单元格,因为该单元格尚未创建,因此它的内容也没有创建。
If your tableview is a static one, only then will the storyboard content of a cell will be shown and you will get the control inside the cell with that tag. 如果您的表格视图是静态的,则只有这样,才会显示单元格的情节提要的内容,并且您将获得带有该标记的单元格内的控件。
I think your tableview is a prototype one, and as because the cell isnt being created so your [self.tableview viewWithTag 10000816]
is returning nil as that control has not been created. 我认为您的tableview是一个原型,并且由于未创建单元格,因此您的
[self.tableview viewWithTag 10000816]
返回nil,因为尚未创建该控件。
Hope could make you understand 希望可以让你理解
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.