我在UILabel中粘贴了一些粘贴文本的错误

时间:2013-10-25 06:57:35

标签: ios json xcode uitableview cell

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

enter image description here

但是当我试图滚动我的表时,我对时间文本有一些问题:

enter image description here

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

我添加了我的代码tableView

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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