Cocoa touch 如何在UITableViewCell中自定义附件披露图像?
我想在我的UITableView中使用标准披露附件映像的自定义版本。我该怎么做?我希望对UITableViewCell进行子分类对于这样的基本操作不是必需的。您需要创建一个自定义视图,并将其分配给Cocoa touch 如何在UITableViewCell中自定义附件披露图像?,cocoa-touch,uitableview,Cocoa Touch,Uitableview,我想在我的UITableView中使用标准披露附件映像的自定义版本。我该怎么做?我希望对UITableViewCell进行子分类对于这样的基本操作不是必需的。您需要创建一个自定义视图,并将其分配给UITableViewCell对象的accessoryView属性。比如: myCell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"Somethin
UITableViewCell
对象的accessoryView
属性。比如:
myCell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"Something" ]];
我遇到了与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 ] ;
我使用上面给出的帮助代码来实现这一点。注意:不要将自定义单元格设置为标记:0。该标记是为textLabel视图保留的。如果在Xcode情节提要中设置标记,则必须将自定义视图/标签的标记设置为1或更高。 请注意苹果的例子:
- (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;
}
我会这样做:
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;
请在否决投票前试试这个!因为现在它有两张赞成票和两张反对票 我尝试了上面的代码,但它似乎不再起作用,可能是因为这篇文章的年代太长了,所以我将放弃我的线路,它为我提供了定制的附件视图
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowBtn"]]];
希望有人觉得这很有用。这里有一个演示如何使用UIControl
来实现这种访问视图定制
在UIControl中覆盖-(void)drawRect:(CGRect)rect
不是最简单的方法,但为您提供了很多灵活性来设计漂亮的附件视图
Swift 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...
}
最棒的是,这让我可以用来处理按钮动作
我用这个按钮,但同样的技术也适用于公开图像,[因为/只要]在您想要触摸的元素的层次结构树的分支中只有一个类
哦,对了,我的代码称之为:
extension UIView {
var allSubviews: [UIView] {
return subviews.flatMap { [$0] + $0.allSubviews }
}
}
在swift 4和5中
myCell.accessoryView = UIImageView(image: UIImage(named: "Something"))
如果我这样做,我注意到UITableViewDelegate方法-[tableView:accessoryButtonTappedForRowWithIndexPath:]不再被调用。这是预期的行为吗?有什么方法可以让这个方法继续工作吗?有更多的解释,在UITableViewCell上使用UIApprance代理可以吗?我使用这种方法来子类UITableViewCell(DisclosureCell),然后所有带有自定义披露指示符的单元格都继承自DisclosureCell。为什么不使用带有图像的简单UIButton,而不是摆弄图层内容?我觉得我一定试过了,但由于某种原因它不起作用?但当然,请先尝试使用UIButton…当使用自定义图像时,位置可能会更改,用户可能无法单击它,因为它的边框可能很小。最好是在附件视图上方添加一个子视图按钮,这样可以处理尺寸和位置。Tom,这个条目的公认答案完全相同。我错过了什么?
myCell.accessoryView = UIImageView(image: UIImage(named: "Something"))