2012-02-12 206 views
1

我正在使用方法cellForRowAtIndexPath來繪製帶有2個標籤的單元格。當我重複使用一個單元格時,我在該單元格中的標籤沒有被刪除,所以我得到兩個標籤重疊,因爲我正在重新使用一個單元格。這很奇怪,因爲只有標籤標題重疊,另一個不是。具有重疊單元格的自定義單元格

所以我做了什麼來解決這個問題,我總是在創建一個新的單元格。哪個不合適,但解決問題...任何人都有類似的錯誤,並設法解決它?因爲我剛剛作出了一個解決辦法...

//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

在方法的開始,通過所有的UIView對象枚舉[cell.contentView subviews]和擺脫他們,就像這樣:

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

這是可行的,因爲所有UI元素都繼承自UIView,因此可以像移除它們一樣被刪除。

0

第一,取消這條線:

// if (cell == nil) 

,如果你不這樣做,你只是創建和每次滾動的時間分配一個新的單元格,從未釋放舊的......

+0

如果我這樣做,我會得到我遇到的問題,因爲我正在重新使用一個單元格。 – bruno 2012-02-12 16:37:41