更新后,Ui标签值在表视图中重叠

时间:2013-10-07 08:19:06

标签: iphone

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

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

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    //clear all the cells subview before drawing the cell
    for(UIView *view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIView class]])
        {
            [view removeFromSuperview];
        }
    }

    if (arrPlaceId.count>0)
    {


        lblPlacename=[[UILabel alloc]initWithFrame:CGRectMake(30, 3, 300, 20)];
        lblPlacename.font=[UIFont fontWithName:@"arial" size:15];
        lblPlacename.textColor=[UIColor whiteColor];

        lblPlacename.backgroundColor=[UIColor clearColor];

        lblAddress=[[UILabel alloc]initWithFrame:CGRectMake(30, 20, 300, 18)];
        lblAddress.font=[UIFont fontWithName:@"arial" size:15];
        lblAddress.textColor=[UIColor whiteColor];

        lblAddress.backgroundColor=[UIColor clearColor];


        lblPlacename.text=[arrPlaceName objectAtIndex:indexPath.row];
        lblAddress.text=[arrPlaceAddress objectAtIndex:indexPath.row];


        [cell addSubview:lblPlacename];
        [cell addSubview:lblAddress];
    }



    return cell;
}

1 个答案:

答案 0 :(得分:0)

将此添加到您的方法

for(id obj in cell.contentView.subviews)
    {
        if([obj isKindOfClass:[UILabel class]])
        {
            [obj removeFromSuperview];
        }
    }

而不是

for(UIView *view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIView class]])
        {
            [view removeFromSuperview];
        }
    }