简体   繁体   中英

Graphics in UITableView cells disappearing

Got a strange problem where I'm putting some text into a cell (using cell.textLabel) and a small "tick" graphic to the right of the cell. When I select the cell, the tick is supposed to appear, or disappear if it's already there. What actually happens is the tick appears then fades out again almost instantly. It's all pretty standard code, so if anyone's got any idea what's going on I'd be pleased to hear!

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

NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
    while ([[cell.contentView subviews] count] > 0) {
        UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
        [labelToClear removeFromSuperview];
    }
}


NSString *theName = [[contacts objectForKey:[contactsKeys objectAtIndex:section]] objectAtIndex:row];

cell.textLabel = theName;


if (section == 0) {
    cell.textLabel.textAlignment = UITextAlignmentCenter;
} else {
    cell.textLabel.textAlignment = UITextAlignmentLeft;
}


if ([selectedContacts containsObject:theName]) {

    CGFloat cellRight = tableView.frame.size.width - 70;

    UIImage *theTickImage = [UIImage imageNamed:@"Tick.png"];
    UIImageView *theTickImageView = [[[UIImageView alloc] initWithImage:theTickImage] autorelease];
    theTickImageView.frame = CGRectMake(cellRight, 10, theTickImage.size.width, theTickImage.size.height);

    [cell.contentView addSubview:theTickImageView];

}


return cell;

}

Many thanks for any help!

Well, I've figured it out - the cell.textLabel was sitting on top of my graphic, so obscuring it. I just set the backgroundColor property of the textLabel to [UIColor clearColor] and all was well - maybe I could have made my graphic sit on top of the textLabel, I haven't tried that yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM