标签文字混乱 [英] Label text getting mixed up

查看:33
本文介绍了标签文字混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示的tableView包含 label .它们将作为每个单元格的 subView 添加.

The tableView displayed below contains label. They are added as subView for each cell.

当我第一次运行程序时,屏幕上显示的单元格具有正确的文本.但是,当我滚动表格时,标签中的文本开始变得混乱.据我了解,创建一个单元格时,一个单元格的标签会覆盖在另一个单元格的顶部.在后两个屏幕截图中已显示了这一点.每次滚动之后,不同的单元格会叠加在另一个单元格的顶部.(因此没有固定的模式.)我试图通过观察哪个单元格正在退出屏幕以及哪些单元格正在进入来了解问题.我试图将这些信息与单元格上的标签相关联,但我听不懂.

When I run my program for the first time, the cells that appear on the screen have the correct text. But when I scroll the table, the text in the labels begin to get mixed up. From what I understand the label of one cell is overlaid on top of another when the cell is created. This has been shown in the latter two screenshots. After each scroll, the different cells are overlaid on top of the other. (So there is no fixed pattern.) I tried to understand the problem by observing which cell which were going out of the screen and the ones which were entering. I tried to correlate this information with the labels on the cells, but I am not able to understand.

我请您帮助我了解造成这种差异的原因.

I request you to kindly help me understand the cause for this discrepancy.

谢谢!

我的代码的相关部分如下:

The relevant section of my code is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSLog(@"s = %d, r = %d", indexPath.section, indexPath.row);

    if (indexPath.section == 0)
    {
        switch(indexPath.row)
        {
            case 0:
            [cell addSubview:classA];
            break;

            case 1:
            [cell addSubview:classB];
            break;

        case 2:
            [cell addSubview:classC];
            break;

        case 3:
            [cell addSubview:classD];
            break;

        case 4:
            [cell addSubview:classE];
            break;

        default:
            break;
    }
}

if(indexPath.section == 1)
{
    switch(indexPath.row)
    {
        case 0:
            [cell addSubview:classF];
            break;

        case 1:
            [cell addSubview:classG];
            break;

        case 2:
            [cell addSubview:classH];
            break;

        default:
            break;
    }
}

if(indexPath.section == 2)
{
    switch (indexPath.row)
    {
        case 0:
            [cell addSubview:classI];
            break;

        case 1:
            [cell addSubview:classJ];
            break;

        case 2:
            [cell addSubview:classK];
            break;

        default:
            break;
    }
}

return cell;
}

推荐答案

原因是,您正在使用 dequeueReusableCellWithIdentifier 重用单元格.因此,在滚动时,超出可见区域的单元格将重新用于当前的可见单元格.

The reason is, you are using dequeueReusableCellWithIdentifier to reuse the cells. So when you are scrolling, the cells which are going out of visible area are getting reused for the current visible cells.

因为您已经在这些单元格中添加了 classB 等标签,此后并没有删除,并且此后在添加 classI 等标签时也未删除.在相同的重用单元格上,它被绘制在此标签的顶部.由于这些标签将背景色设置为透明色,因此还会显示在其后面绘制的标签,从而产生重叠的感觉.

Since you have already added labels like classB etc.. in these cells and not removed after that, and after that when you are adding labels such as classI etc.. on the same reused cells, it is getting drawn on top of this label. Since these labels have background color set as clear color, it will show the label which was drawn behind it also and hence giving an overlapping feeling.

根据屏幕截图,所有单元格看起来都是相同的,您绝对可以使用 dequeueReusableCellWithIdentifier 重用这些单元格.那么,为什么不更改在 cellForRowAtIndexPath 外部创建标签的逻辑,而只将其用作 cell.TextLabel.text = @"Class B"; 等.该方法中是否存在条件?

As per the screenshot, all the cells are looking identical and you can definitely reuse the cells with dequeueReusableCellWithIdentifier. So why dont you change your logic of creating the label outside cellForRowAtIndexPath and instead just use it as cell.TextLabel.text = @"Class B"; etc.. in the corresponding if conditions inside this method?

您的代码将如下所示,

if (indexPath.section == 0)
    {
        switch(indexPath.row)
        {
            case 0:
            cell.textLabel = [self createLabelForRow:indexPath.row section:indexPath.section];
//or if it is jut a simple label, you can just set font and other properties in cell.textLabel directly
            cell.textLabel.text = @"Class A";
            break;

更新:如果所有单元格都是静态的,这是对 dequeueReusableCellWithIdentifier 重用的不太好的解决方法.但这不是推荐的方法.

Update: Here is a not-so-good fix for dequeueReusableCellWithIdentifier reuse in case all cells are static. But this is not a recommended approach.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  NSString *CellIdentifier = [NSString stringWithFormat:@"CellForRow%dSection%d",indexPath.row, indexPath.section];
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil)
   { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
   }

  //or try this

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
  if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

Update2 :因此,如果您遇到上述问题,请尝试此操作.使用我的第一种方法,然后执行以下操作.

Update2: So if you are facing issues with above try this. Use my first approach and do the following.

if (indexPath.section == 0)
    {
        switch(indexPath.row)
        {
            case 0:
            cell.textLabel = [self createLabelForRow:indexPath.row section:indexPath.section];

            // or do the following,

            [self removeAllTextFieldsFromCell:cell];
            UITextField *aTextField = [self createTextFieldForRow:indexPath.row section:indexPath.section];
            [cell.contentView addSubview:aTextField];
         }

removeAllTextFieldsFromCell:方法中使用R.A的建议.

In removeAllTextFieldsFromCell: method use the R.A's suggestion.

例如:-

- (void)removeAllTextFieldsFromCell:(UITableViewCell *)cell {

    for (UITextField *textField in cell.contentViews.subViews) {
        [textField removeFromSuperView];
    }
}

或者只是使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSLog(@"s = %d, r = %d", indexPath.section, indexPath.row);

    //if it is all textfields in the cell, it will look like,
    [self removeAllTextFieldsFromCell:cell];
    UITextField *aTextField = [self createTextFieldForRow:indexPath.row section:indexPath.section];
    [cell.contentView addSubview:aTextField];

    return cell;
}

这篇关于标签文字混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆