简体   繁体   English

如何覆盖 UITableViewCell 中的附件视图来改变它的位置

[英]How to override accessoryView in UITableViewCell to change it's position

By default the accessoryView on the UITableViewCell is positioned to the far right on the cell.默认情况下, UITableViewCell上的accessoryView UITableViewCell位于单元格的最右侧。 I am sub-classing the UITableViewCell to try and change this position to move it over more the left, however, it has no effect and it remains to the right.我正在对UITableViewCell进行子类化以尝试更改此位置以将其移动到更多左侧,但是,它没有任何效果,它仍然在右侧。 Any ideas on how to do this?关于如何做到这一点的任何想法?

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.accessoryView.frame = CGRectMake(100, self.accessoryView.frame.origin.y, self.accessoryView.frame.size.width, self.accessoryView.frame.size.height);
    self.accessoryView.layer.borderWidth = 1;
    self.accessoryView.layer.borderColor = [[UIColor redColor] CGColor];
}

在此处输入图片说明

在单元子类中实现layoutSubviews ,首先调用 super ,然后修改要更改的子视图的框架。

This is similar to Flea's solution;这类似于 Flea 的解决方案; pushes the image by the given amount instead of a fixed frame:按给定数量而不是固定帧推动图像:

static CGFloat const kRightOffset = 10.0;

cell.accessoryView = ({UIImageView * imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image.png"]];
                CGRect frame = imgV.frame;
                frame.size.width = frame.size.width + kRightOffset;
                imgV.frame = frame;
                [imgV setContentMode:UIViewContentModeLeft];
                imgV; });

Based on Matt's tip the only way I found to do this was to just use my own UIImageView and use the image of my choice and then set the frame coordinates to what I needed.根据马特的提示,我发现这样做的唯一方法是使用我自己的UIImageView并使用我选择的图像,然后将frame坐标设置为我需要的。 This allowed the accessoryView to expand.这允许accessoryView视图扩展。 Apparently this is the only way to manipulate it.显然,这是操纵它的唯一方法。

UIImage *indicatorImage = [UIImage imageNamed:@"BV-icon_57x57"];
        UIImageView *view = [[UIImageView alloc] initWithImage:indicatorImage];
        view.frame = CGRectMake(0, 0, 100, 20);
        [view setContentMode:UIViewContentModeLeft];//without this line the image will just be stretched;
        cell.accessoryView = view;

The easiest way would be to define a custom cell, set Accessory to none, place an imageview that acts on behalf of the accessory view wherever you like, and define a tap gesture handler for the cell (if you want to limit tap gesture to only the image, just add tap gesture only to imageview instead of entire cell).最简单的方法是定义一个自定义单元格,将 Accessory 设置为 none,在您喜欢的任何地方放置一个代表附件视图的 imageview,并为单元格定义一个点击手势处理程序(如果您想将点击手势限制为仅图像,只需将点击手势添加到图像视图而不是整个单元格)。 This gives a lot of flexibility, plus we don't have to deal with the annoyances that accessoryview brings (for me at least - i dislike the border on the right of the cell)这提供了很大的灵活性,而且我们不必处理附件视图带来的烦恼(至少对我来说 - 我不喜欢单元格右侧的边框)

In your custom cell setup code:在您的自定义单元设置代码中:

let tap = UITapGestureRecognizer(target: self, action: #selector(onCellTapped(_:)))
self.contentView.addGestureRecognizer(tap)

@objc private func onCellTapped(_: Any) {
   //your code here - i added an imageview that changes image on user tap - to indicate selected/deselected
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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