【问题标题】:Reload TableViewCell's UILabels in dynamic table在动态表中重新加载 TableViewCell 的 UILabel
【发布时间】:2012-02-20 08:44:16
【问题描述】:

我得到了带有 UITableView 的应用程序。

每个 UITableViewCell 中都有 UILabel。我是这样添加标签的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //look at the upd
}

但是当我尝试使用其他信息重新加载此表中的数据时,旧信息(旧标签)不会消失,但仍在单元格上。

看起来像这样:...

我应该如何组织添加 UILabel?

更新

我以这种方式更正代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent Dream";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:16];
        [textLabel setTag:(int)indexPath];
        [cell.contentView addSubview:textLabel];


    }
    // Configure the cell...

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];

    cell.contentView.backgroundColor = background;

    UILabel *textLabel = (UILabel *)[cell viewWithTag:(int)indexPath];
    textLabel.text = tempDream.title;

    NSLog(@"%@",tempDream.title);
    NSLog(@"%@",textLabel.text);

    cell.textLabel.text = @"";
    cell.detailTextLabel.text = stringFromDate;

    return cell;
}

输出如下所示:

2012-02-20 15:58:10.512 DreamerIOS[3037:10403] lllooooonggg dreeeeeeeaaaaammmm yeeeeeeah
2012-02-20 15:58:10.513 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] dream to end
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] adsfhadskgh dfagfadkuy gadyv dsfdfad fsdf a ghfncdhg hjfg f dfj ghf 
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] (null)

所以,NSLog(@"%@",tempDream.title);可以,但是 NSLog(@"%@",textLabel.text);一片空白。为什么?

【问题讨论】:

    标签: ios xcode uitableview uilabel


    【解决方案1】:

    这是因为每次给单元格添加一个新标签。

    当您初始化一个新单元格时,您添加标签并为其设置标签

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        //  add label here
        ...
        [label setTag:999];
    }
    

    然后在配置单元格的时候,使用[UIView viewWithTag:]获取标签的引用

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:999];
    

    更新:

    您应该为标签使用一个常量值。如果您使用 indexPath 作为标记,则可能找不到视图。由于您正在重用单元格,因此系统只会创建一定数量的单元格。当单元格不在视野范围内(不可见)时,dequeueReusableCellWithIdentifier 将为您获取单元格,而不是创建一个新单元格。

    reusableCell 是您之前创建的视图。 您为标签使用动态值的情况如下: 您在单元格 10(indexPath),系统会找到一个在 3(indexPath) 处创建的 reusableCell。由于是在3创建的,label标签是3。但是你发现一个view标签是10,所以结果是null。

    【讨论】:

    • [textLabel setTag:1];和 UILabel *textLabel = (UILabel *)[cell viewWithTag:1];但仍然为空 =(
    • 对不起我的错误,应该是 [cell.contentView viewWithTag:tag]
    • 谢谢。但它应该被替换 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];到 UITableViewCell *cell =) 并且工作完美
    【解决方案2】:

    这是因为您没有正确重用表格视图单元格。 您必须在以下代码块中创建标签
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    【讨论】:

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