【问题标题】:creating label on uitablview cell in iphone?在 iphone 的 uitablview 单元格上创建标签?
【发布时间】:2012-12-14 10:09:42
【问题描述】:

我是 iPhone devlopemnt 的新手,我正在开发一个应用程序。在该应用程序中,我需要在UITableView 中显示列表项及其各自的价格。为了解决这个问题,我遵循了一些概念,比如在UITableViewCell 中动态创建标签以显示商品及其价格。

在应用程序中,第一个 indexid == 1 表示我在单击列表中的特定单元格时显示列表位置,我正在获取最喜欢的项目列表,其价格意味着 indexid = 2 ...点击后退按钮 indexid = 1 然后我需要隐藏该标签...但标签没有隐藏它显示标价而不是项目列表

lblText.text = nil;
btnBack.hidden = FALSE;
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{     
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
     cell.textLabel.highlightedTextColor = [UIColor orangeColor];

}

    /////////////////////   Cell Title    /////////////////////
    if (indexId == 1) 
    {
//        NSLog(@"%@",lblText1);
       cell.textLabel.text = [NSString stringWithFormat:@"%@", [test.arrTitle objectAtIndex:indexPath.row]];
        lblText = [[UILabel alloc] initWithFrame:CGRectMake(250, 7, 40, 30)]; // For right alignment
        lblText.text = [test.arrId objectAtIndex:indexPath.row];
        [lblText setTextAlignment:UITextAlignmentCenter];
        lblText.textColor = [UIColor redColor];
        lblText.backgroundColor = [UIColor clearColor];
        [cell addSubview:lblText];

       cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"products-category-bg.png"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [lblText release];

    }
    else
    {
        lblText.hidden = YES;
        cell.textLabel.text = [NSString stringWithFormat:@"  %@", [test.arrTitle objectAtIndex:indexPath.row]];        

            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product-bg-hover.png"]];
    }       
    return cell;
}

我有以下问题。标签创建正常,但单击后退按钮后,我在主表视图中获得标签值。

如何释放带有数据的标签对象?

感谢和问候

请帮我解决这个问题

【问题讨论】:

    标签: objective-c ios json


    【解决方案1】:

    您的代码对我来说很好,只需将上面代码中的一行替换为下面的链接

    [cell.contentView addSubview:lblText];
    

    相反

    [cell addSubview:lblText];
    

    希望它能解决您的问题。

    【讨论】:

    • thanq 快速重播..但我得到同样的错误...这是 uiatblview 第一个单元格上的标签仅显示价格..
    【解决方案2】:

    如果您对应用程序中的不同状态使用相同的表格视图实例,那么您也在重复使用之前创建的单元格:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    对于每个单元格,当indexid == 1 你也在这样做:

    [cell addSubview:lblText];
    

    对我来说,这表示您创建的每个单元格(并且将被重复使用)都有一个 UILabel 添加为子视图。要真正重用该单元格,您需要在您的逻辑表明您需要再次添加它之前删除该 UILabel。这不仅会在您不需要时删除单元格,还会阻止您在运行时向单元格添加额外的 UILabel 实例。在检查indexid之前尝试添加下面的代码

    if (cell == nil)
    {     
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
         cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    }
    
    // Be sure to remove the UILabels on the re-used cell.
    for(UIView *view in cell.subviews)
        if ([view isKindOfClass:([UILabel class])])
            [view removeFromSuperview];
    
    if (indexid == 1) { ....
    

    这将在每次创建单元格时删除 UILabel,允许您有条件地将其重新添加为子视图。

    【讨论】:

      【解决方案3】:

      尝试为你的标签设置一个标签(将帮助你将数据设置为标签,你可以通过调用来获取标签

      [单元格 viewWithTag:theLabelTag]

      其中theLabelTag标签可以是索引) 并在

      中设置标签值
      tableView: willDisplayCell: forRowAtIndexPath:
      

      而不是

      tableView: cellForRowAtIndexPath:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-13
        • 1970-01-01
        相关资源
        最近更新 更多