0

我在 tableViewCell 中创建的 UILabel 有一些问题。所以我有带有Textlabel、详细文本和我第三个创建的单元格标签的tableView。所以我从 JSON 获取信息并粘贴到第三个标签,所以它的结果:

在此处输入图像描述

但是当我尝试滚动我的表格时,我的时间文本出现了一些问题:

在此处输入图像描述

所以当我在这个标签上添加 UIColor 白色作为背景时,我没有这个白痴在数字后面。请帮助我做错了什么?

我添加我的代码表视图

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



    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];




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

               cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

    }

    cell.textLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"song"];

    cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"artist"];

    UILabel *mainLabel;

    //frame=CGRectMake(Позиция по X, Позиция по Y, Размеры ширина, Раздел высота)

    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 58, 220.0, 15.0)] autorelease];

    mainLabel.font = [UIFont systemFontOfSize:14.0];

    mainLabel.textColor = [UIColor blackColor];
    [cell.contentView addSubview:mainLabel];

    mainLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"start"];

    [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"1@2x.png"]];



    cell.textLabel.textColor = [UIColor whiteColor];

    cell.detailTextLabel.textColor = [UIColor whiteColor];

    mainLabel.backgroundColor = [UIColor clearColor];

    mainLabel.textColor = [UIColor whiteColor];

    [tableTrack setBackgroundView:nil];

    [TrackTableView setBackgroundView:nil];

    tableTrack.backgroundColor = [UIColor clearColor];

    TrackTableView.backgroundColor = [UIColor clearColor];

    UIFont *f = [UIFont fontWithName:@"Helvetica 35 Thin" size:18.0f];

    cell.textLabel.font = f;

    UIFont *f2 = [UIFont fontWithName:@"Helvetica 35 Thin" size:14.0f];

    cell.detailTextLabel.font = f2;

    UIFont *f3 = [UIFont fontWithName:@"Helvetica 35 Thin" size:14.0f];

    mainLabel.font = f3;


    self.tableTrack.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(10, 74, 1024, 1)];
    separatorView.layer.borderColor = [UIColor whiteColor].CGColor;
    separatorView.layer.borderWidth = 0.5;
    [cell.contentView addSubview:separatorView];

    self.TrackTableView.separatorStyle = UITableViewCellSeparatorStyleNone;





    return cell;
}
4

2 回答 2

1

试试这个委托方法..问题是你添加标签到dequeed单元格只在创建它们时添加子视图......试试这个代码..

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



        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];






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


         mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 58, 220.0, 15.0)] autorelease];

            mainLabel.font = [UIFont systemFontOfSize:14.0];

            mainLabel.textColor = [UIColor blackColor];
            [cell.contentView addSubview:mainLabel];

        UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(10, 74, 1024, 1)];
            separatorView.layer.borderColor = [UIColor whiteColor].CGColor;
            separatorView.layer.borderWidth = 0.5;
            [cell.contentView addSubview:separatorView];
            }

            cell.textLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"song"];

            cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"artist"];

            UILabel *mainLabel;

            //frame=CGRectMake(Позиция по X, Позиция по Y, Размеры ширина, Раздел высота)


            mainLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:@"start"];

            [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"1@2x.png"]];



            cell.textLabel.textColor = [UIColor whiteColor];

            cell.detailTextLabel.textColor = [UIColor whiteColor];

            mainLabel.backgroundColor = [UIColor clearColor];

            mainLabel.textColor = [UIColor whiteColor];

            [tableTrack setBackgroundView:nil];

            [TrackTableView setBackgroundView:nil];

            tableTrack.backgroundColor = [UIColor clearColor];

            TrackTableView.backgroundColor = [UIColor clearColor];

            UIFont *f = [UIFont fontWithName:@"Helvetica 35 Thin" size:18.0f];

            cell.textLabel.font = f;

            UIFont *f2 = [UIFont fontWithName:@"Helvetica 35 Thin" size:14.0f];

            cell.detailTextLabel.font = f2;

            UIFont *f3 = [UIFont fontWithName:@"Helvetica 35 Thin" size:14.0f];

            mainLabel.font = f3;


            self.tableTrack.separatorStyle = UITableViewCellSeparatorStyleNone;



            self.TrackTableView.separatorStyle = UITableViewCellSeparatorStyleNone;





            return cell;
        }
于 2013-10-25T07:36:35.293 回答
0

错误是您在 if 条件 if(cell==nil) 之外的(其他部分)中分配了标签。您应该只在首次创建单元格时创建一次标签。

当我们运行代码时,控件会检查单元格是否为 nil 或有任何值。如果我们每次都在 else 部分分配标签(同时滚动),那么您每次都在创建一个新标签。如果我们在 if 条件下分配它,标签只为每个单元格分配一次,我们只是在滚动时更新文本值。

于 2013-10-25T07:29:08.777 回答