ios / xcode / objective-c:如何在自定义单元格中创建带图像的附件视图?

时间:2015-06-11 00:38:47

标签: ios tablecell accessoryview

我的理解是,要显示带有图像的附件视图,您需要更改cellForRowWithIndex路径。

基于SO中的各种教程和问题,除了厨房外,所有东西都抛出了这个方法。有人说你只需要包括配件视图。其他人说你需要一个按钮。无论如何,图像仍未显示。

我也被告知这个问题不在故事板中,就像在一个股票配件视图中而不是没有。

任何人都可以推荐一种可靠的方法来让图像显示在附件视图中:

这是我尝试过的方法没有成功:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   // UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    IDContactImport *contactImport = self.contacts[indexPath.row];
    IDTableViewCell *idTableCell =(IDTableViewCell *)cell;
    idTableCell.contactImport=contactImport;
    if (contactImport.imageData==nil)
    {
        idTableCell.iconView.image = [UIImage imageNamed:@"headshot.jpg"];
    }
    else{
       idTableCell.iconView.image = [UIImage imageWithData:contactImport.imageData];
    }
    UIImage *image = [UIImage imageNamed:@"checked.gif"];

    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 addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;


    NSLog(@"Trying to load accessory image");

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.gif"]];
    cell.accessoryView = imageView;
  //  UIImage *image = [UIImage   imageNamed:@"headshot.jpg"];

 //   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 addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    return cell;
}

1 个答案:

答案 0 :(得分:0)

你有正确的答案。但它会在代码中被破坏。

这是正确的:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.gif"]];
cell.accessoryView = imageView;

此外,使用PNG图像,而不是GIF。它是iOS开发的更好格式。