如何在UITableViewCell中自定义配件显示图像?

时间:2008-12-31 18:40:35

标签: cocoa-touch uitableview

我想在我的UITableView中使用标准显示配件图像的自定义版本。我怎样才能做到这一点?我希望对这个基本的东西不需要对UITableViewCell进行子类化。

9 个答案:

答案 0 :(得分:121)

您需要创建自定义视图并将其分配给accessoryView对象的UITableViewCell属性。类似的东西:

myCell.accessoryView = [[ UIImageView alloc ] 
                       initWithImage:[UIImage imageNamed:@"Something" ]];

答案 1 :(得分:9)

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

    UIImage *indicatorImage = [UIImage imageNamed:@"Arrow.png"];
    cell.accessoryView =[[[UIImageView alloc]  initWithImage:indicatorImage] autorelease];


    return cell;
}

我使用上面给出的帮助代码

答案 2 :(得分:7)

我遇到了与Greg相同的问题 - 附件视图没有跟踪(如果你使用UIImageView)

我解决了这个问题:

UIImage * image = [ UIImage imageNamed:@"disclosure-button-grey.png" ] ;
UIControl * c = [ [ UIControl alloc ] initWithFrame:(CGRect){ CGPointZero, image.size } ] ;

c.layer.contents = (id)image.CGImage ;
[ c addTarget:self action:@selector( accessoryTapped: ) forControlEvents:UIControlEventTouchUpInside ] ;
cell.accessoryView = c ;
[ c release ] ;

答案 3 :(得分:0)

我会这样做:

UIImageView *chevronImgVw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron_accessory_vw_img.png"]];
chevronImgVw.frame = CGRectMake(cell.accessoryView.frame.origin.x, cell.accessoryView.frame.origin.y, 10, 20);
cell.accessoryView = chevronImgVw;

请在投票前试试这个!因为目前它有2个upvotes和2个downvotes!

答案 4 :(得分:0)

有一个nice example from Apple显示了如何使用UIControl来实现这种accessoryView自定义。

在UIControl中覆盖- (void)drawRect:(CGRect)rect并不是最简单的方法,但为您提供了很多灵活性来设置漂亮的配件视图。

enter image description here

答案 5 :(得分:0)

迅速4和5:

这对我有用:

class MyCell: UITableViewCell {

// boilerplate...

    fileprivate func commonInit() {
        // This is the button we want, just need to change the image.
        accessoryType = .detailButton
    }

    open override func layoutSubviews() {
        super.layoutSubviews()
        // Grab the "detail" button to change its icon. Functionality is set in the delegate.
        if let submitButton = allSubviews.compactMap({ $0 as? UIButton }).first {
            submitButton.setImage(#imageLiteral(resourceName: "icons8-enter-1"), for: .normal)
        }
    }

   // everything else...
}

最重要的是,这使我可以使用tableView(_:accessoryButtonTappedForRowWith:)来处理按钮操作。

我将其用于按钮,但相同的技术也适用于公开图像,[因为/只要]您要触摸的元素的层次结构树的该分支中只有一个类。


哦,对了,我的代码称为:

extension UIView {
    var allSubviews: [UIView] {
        return subviews.flatMap { [$0] + $0.allSubviews }
    }
}

答案 6 :(得分:0)

快速4和5

myCell.accessoryView = UIImageView(image: UIImage(named: "Something"))

答案 7 :(得分:-1)

我尝试了上面的代码,它似乎不再起作用了,可能是由于这篇文章的年龄,所以我会抛弃我给自定义配件视图的线。

[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowBtn"]]];

希望有人觉得这很有用。

答案 8 :(得分:-2)

注意:请勿将自定义单元格设置为标记:0。该标记是为textLabel视图保留的。如果您在Xcode Storyboard中设置了代码,则必须将自定义视图/标签的代码设置为1或更高。

注意Apple的例子:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    UILabel *label;

    label = (UILabel *)[cell viewWithTag:1];
    label.text = [NSString stringWithFormat:@"%d", indexPath.row];

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];

    return cell;
}
相关问题