您的位置:首页 > 其它

Label in [cell viewWithTag:] is always returned as nil

2015-12-22 16:39 387 查看
stackoverflowk上面的回答:

方法1、

http://stackoverflow.com/questions/26344106/viewwithtag-returns-nil-on-uitableviewcell-only-first-time

方法2、

http://stackoverflow.com/questions/27815263/label-in-cell-viewwithtag-is-always-returned-as-nil

In your code you are creating a new cell, that it's not the same than you have in Storyboard.

Change this: This is the old way, or the way you use when the cell is by code or nib, and you don't use storyboard. This code means.
NSString * identifier = @"secondReusableIdentifier";
// If I have available a cell with this identifier: secondReusableIdentifier, let's go to use it.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil){
// If not, we create a new cell with this identifier. This methods is previous to storyboard, and this methods create a new cell, but does´t look in Storyboard if this identifier exist, or something like that.

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}


To this, in other hand when Apple launched storyboard the framework grow with this methods, that work in this way: If there is a cell free use it, if not it look in Storyboard for a cell with this identifier and create a new cell with this info. (You can use
this methods also by code and with nib file, but you must register the class before...).
// Be sure than: "secondReusableIdentifier", it's its identifier in storyboard
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondReusableIdentifier" forIndexPath:indexPath];


shareimprove
this answer
edited Jan
7 at 8:57

answered Jan 7 at 8:40





Onik IV
2,1321512

 
 
It worked! But how? Can you explain a little more in depth what is the catch here? – markich Jan
7 at 8:44
 
Ok, I´m going to explain a litle bit in my answer. – Onik
IV Jan
7 at 8:45
上面两种方法我都试过了,都可以
2015-12-25补充:

在使用上面第二种方法的时候,如果给tableview添加新的row,

 [self.tableView
insertRowsAtIndexPaths:array
withRowAnimation:UITableViewRowAnimationAutomatic];
,这样tableview 新添加的行仍然会出现上面的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: