【问题标题】:UILabel with viewWithTag - (text not appearing)带有 viewWithTag 的 UILabel -(文本未出现)
【发布时间】:2014-09-17 10:43:05
【问题描述】:

有一个 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".

当我跑步时,单元格是空的。有什么建议吗?

【问题讨论】:

  • 在您的 cellForRowAtIndexPath 方法中,您指定了 1000 的标签。我假设是错字吗?
  • 这只是错误的人,因为我从互联网上复制了这段代码。我们假设标签是 1000 而不是 5000。感谢您的快速回复
  • 我看不到您在任何地方添加标签。您只是将 UITableViewCell 转换为 UILabel ,这首先是不正确的..
  • 我从 StoryBoard 添加了一个标签并提供了标签号。不正确吗?

标签: ios uitableview uilabel


【解决方案1】:

问题也可能是您没有使用唯一的标签号。这意味着您的视图控制器场景中的某些内容可能具有您在另一个对象上定义的相同标签号。

例如,对我来说,我有一个 UITableViewCell,它的标签 # 为 0,一个 UILabel 的标签 # 为 0,另一个 UILabel 的标签 # 为 1。这导致了问题,并使其成为标签 #1根本不会显示。当我单击该行时,我可以看到文本——但是当没有单击该行时,数据不会显示。

当我更改了两个 UILabel 的标签号时,它解决了这个问题。

【讨论】:

    【解决方案2】:

    您应该检查 [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"][cell viewWithTag:5000] 是否返回 nil 以外的内容。

    如果是,请务必按以下顺序进行:

    - (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"];
        }
    }
    

    【讨论】:

    • 很高兴我能帮上忙! =)
    • @leecan 你检查过你单元格上的标签标识符是否真的是5000吗?
    • 你的单元格的identifier@"ChecklistItem"(或者你正在检查的任何东西)?
    【解决方案3】:

    问题已经解决,我的应用程序在 iPhone 上运行良好,所以我决定让它通用。我将所有视图控制器复制到 iPad 故事板。我开始对 iPad 故事板进行一些更改。

    无论我在 iPad 故事板上添加/删除什么,都没有做任何改变,因为主界面是 iphone,原因是我没有改变 iPad 的主界面,所以在 iPad 上运行时,我使用的是iPhone 的故事板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 2017-06-05
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多