2012-02-12 12 views
1

2つのラベルを持つセルを描画するのにcellForRowAtIndexPathメソッドを使用しています。私がセルを再利用すると、そのセルのラベルは消去されていないので、セルを再利用しているので、2つのラベルが重なってしまいます。これは、ラベルのタイトルだけが重なってしまうので、別のものではありません。オーバーラップするセルを含むカスタムセル

これを修正するために、私はいつも新しいセルを作成しています。これは適切ではないが、問題を解決する...誰もが同様のバグを持っていて、それを修正することができますか?あなたは、あなたは単にあなたがスクロールするたびに作成し、新しいセルを割り当てているしていない場合

// if (cell == nil) 

、および:私はちょうどこの問題を回避する...

//Desenhado por codigo 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
UILabel *label = nil; 
UILabel *label2 = nil;  
UIColor *green = [UIColor colorWithRed:0 green: 0.77 blue:0 alpha:1]; 

cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
// if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease]; 

    //Descrição 
    label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label setLineBreakMode:UILineBreakModeWordWrap]; 
    [label setMinimumFontSize:FONT_SIZE]; 
    [label setNumberOfLines:0]; 
    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:14.0]];   
    [label setTag:1]; 
    [label setTextColor:green];  
    [label setBackgroundColor:[UIColor clearColor]];  
    label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"barra_cinza_lista.png"]];   
    [[cell contentView] addSubview:label]; 

    label2 = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label2 setLineBreakMode:UILineBreakModeWordWrap]; 
    [label2 setMinimumFontSize:FONT_SIZE]; 
    [label2 setNumberOfLines:0]; 
    [label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
    [label2 setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:14.0]];   
    [label2 setTag:2]; 
    [label2 setTextColor:[UIColor whiteColor]];  
    [label2 setBackgroundColor:[UIColor clearColor]]; 

    [[cell contentView] addSubview:label2]; 
} 

NSString *text = [self getTableViewRow:tableView index:indexPath]; 

NSString *title = @" %@"; 

if(tableView == myTableView1) 
    title = [NSString stringWithFormat:title, [_titlesResults objectAtIndex:indexPath.row]]; 
else 
    title = [NSString stringWithFormat:title, [_titlesVision objectAtIndex:indexPath.row]]; 

CGFloat widthMax = CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2); 

CGSize constraint = CGSizeMake(widthMax, 20000.0f); //Tamanho máximo permitido de largura e altura 
CGSize size1 = [title sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  //Tamanho ocupado pelas letras 
CGSize size2 = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  //Tamanho ocupado pelas letras 

if (!label) 
    label = (UILabel*)[cell viewWithTag:1]; 

if (!label2) 
    label2 = (UILabel*)[cell viewWithTag:2];  

[label setText:title]; 
[label setFrame:CGRectMake(0, 0, 320, MAX(size1.height, 44.0f))];  
[label2 setText:text]; 
[label2 setFrame:CGRectMake(CELL_CONTENT_MARGIN, 3 * CELL_CONTENT_MARGIN + size1.height, widthMax, MAX(size2.height, 44.0f))];  

return cell; 
} 

答えて

0

は、[cell.contentView subviews]内のすべてのUIViewのオブジェクトを列挙し、そのように、それらを取り除く:

for (UIView *subview in [cell.contentView subviews]) { 
    [subview removeFromSuperview]; 
} 

これはすべてのUI要素がUIViewから継承されているため、このように削除できます。

0

、最初のコメントを解除し、このラインを作った原因古いものを解放することはありません...メソッドの開始時に

+0

もし私がそうするなら、私はセルを再利用しているので、私が持っていた問題が発生します。 – bruno

関連する問題