带有viewWithTag的UILabel – (文字不出现)
有一个UITableViewCell,我用标签1000添加一个标签。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]; UILabel *label = (UILabel *) [cell viewWithTag:1000]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
然后
UILabel *labelName = (UILabel *)[cell viewWithTag:1000] labelName.text = @"test".
当我跑,细胞是空的。 有什么build议?
你应该检查[tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]
和[cell viewWithTag:5000]
是否返回[cell viewWithTag:5000]
东西。
如果是,请务必按以下顺序进行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Try to reuse the cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]; //If you can't reuse, instantiate the cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChecklistItem"]; } //Now that you have your cell, find the label UILabel *labelName = (UILabel *)[cell viewWithTag:5000]; //Check if the label exists just in case if (labelName) { //And set the text [labelName setText:@"test"]; } }
问题也可能是您没有使用唯一的标签号码。 这意味着你的视图控制器场景中的东西可能有相同的标签号码,你已经定义在另一个对象上。
例如,对于我来说,我有一个UITableViewCell,它的标签号为0,UILabel的标签号为0,另一个UILabel的标签号为1.这引起了一些问题,并将其设置在标签#1所在的位置显示在所有。 当我点击该行时,我可以看到文本 – 但数据不会显示该行未被点击的时间。
当我改变了两个UILabel的标签号码,它解决了这个问题。
这个问题已经解决了,我的应用程序在iPhone上工作得很好,所以我决定让它通用。 我把所有的视图控制器复制到iPad的故事板。 我开始在iPad故事板上做一些改变。
无论我在iPad故事板上添加/删除什么,都没有做任何改变,因为主界面是iPhone,原因是我没有更改iPad的主界面,所以在iPad上运行时,我使用iPhone的故事板。