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):
After (highlighted):
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.
精彩评论