2011-01-27 54 views
0

我已經自定義了一個UITableViewCell的contentView以擁有2個標籤。突出顯示的UITableViewCell - 內容視圖重複

但是,當我選擇/突出顯示單元格時,contentView似乎會自我複製。

這裏的(前)的例子: enter image description here

後(高亮):

enter image description here

這裏是我的單元代碼:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    UILabel *mainLabel = [[[UILabel alloc] init] autorelease]; 
    UILabel *detailedLabel = [[[UILabel alloc] init] autorelease]; 
    [mainLabel setFont:[UIFont boldSystemFontOfSize:18.0f]]; 
    [detailedLabel setFont:[UIFont systemFontOfSize:13.0f]]; 

    mainLabel.frame = CGRectMake(51, 5, 0, 0); 
    mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    detailedLabel.frame = CGRectMake(51, 23, 0, 0); 
    detailedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    NSString *text = [documents objectAtIndex:indexPath.row]; 
    mainLabel.text = [text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", [text pathExtension]] withString:@""]; 
    NSString *extDescription = [self extensionDescriptionFromExtension:[text pathExtension]]; 
    NSString *fileSize = [self getFileSize:[NSString stringWithFormat:@"%@/Documents/%@", documentsDirectory, text]]; 
    detailedLabel.text = [NSString stringWithFormat:@"%@ - %@", fileSize, extDescription]; 
    cell.imageView.image = [UIImage imageNamed:@"txtIcon.png"]; 

    [cell.contentView addSubview:mainLabel]; 
    [cell.contentView addSubview:detailedLabel]; 

    return cell; 

} 

任何幫助表示讚賞。

+3

爲此目的有一個預定義的單元格樣式。爲什麼不使用這個? – Axel 2011-01-27 11:12:15

回答

1

瞭解它與UITableViewCellStyleSubtitle一起工作。 enter image description here