代码之家  ›  专栏  ›  技术社区  ›  pankaj

更改uitableviewcell附件图像

  •  2
  • pankaj  · 技术社区  · 15 年前

    是否可以更改tableviewcell中的uitableviewcell附件图像??

    2 回复  |  直到 15 年前
        1
  •  1
  •   Gyani    15 年前

    ,在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法使用此代码

    UIImage *image = [UIImage imageNamed:@"yourImage.png"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    
        2
  •  2
  •   Matthias Bauch    15 年前

    当然是。只需替换整个附件视图

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        // setup cell
        UIImageView *aView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]] autorelease];
        cell.accessoryView = aView;
        return cell;
    }
    

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath        
        // setup cell
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 44, 44);
        [button setImage:[UIImage imageNamed:@"SettingsIcon.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(accessoryButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;
        return cell;
    }
    
    - (void)accessoryButton:(UIControl*)button withEvent:(UIEvent *)event {
        UITableView *tv = (UITableView*)[[button superview] superview];
        UITableViewCell *cell = (UITableViewCell*)[button superview];
        NSIndexPath *ip = [tv indexPathForCell:cell];
        [tv.delegate tableView:tv accessoryButtonTappedForRowWithIndexPath:ip];
    }