开发者

UITableViewCell Highlighted - Content View duplicating

开发者 https://www.devze.com 2023-02-07 14:03 出处:网络
I\'ve customized a UITableViewCell\'s contentView to have 2 labels. However, when I select/highlight the cell the contentView seems to duplicate itself.

I've customized a UITableViewCell's contentView to have 2 labels.

However, when I select/highlight the cell the contentView seems to duplicate itself.

Here's an example (before):

UITableViewCell Highlighted - Content View duplicating

After (highlighted):

UITableViewCell Highlighted - Content View duplicating

Here's my code for the cell:

- (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 strin开发者_C百科gWithFormat:@"%@/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;

}

Any help appreciated.


Got it working with UITableViewCellStyleSubtitle.

UITableViewCell Highlighted - Content View duplicating

0

精彩评论

暂无评论...
验证码 换一张
取 消