如何在UITableViewCell中定制附件披露图像?
问题描述:
我想在我的UITableView中使用标准披露附件图像的自定义版本。我怎样才能做到这一点?我希望子类UITableViewCell对于这个基本的东西不是必需的。如何在UITableViewCell中定制附件披露图像?
答
您需要创建自定义视图并将其分配给UITableViewCell
对象的accessoryView
属性。喜欢的东西:
myCell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"Something" ]];
答
我遇到同样的问题,因为格雷格 - 附属视图不跟踪(如果你使用的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 ] ;
答
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *indicatorImage = [UIImage imageNamed:@"Arrow.png"];
cell.accessoryView =[[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
return cell;
}
以及我做到这一点上面
答
注意给予帮助代码:不设置自定义单元格标记:0。该标记是为textLabel视图保留的。如果您在Xcode Storyboard中设置了标签,您必须将自定义视图/标签的标签设置为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;
请尝试在此之前你失望的一票!因为目前它有2个upvotes和2个downvotes!
答
我试过上面的代码,它似乎不工作了,也许是由于这篇文章的年龄,所以我会抛弃我的行,给了我定制的配件视图。
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowBtn"]]];
希望有人认为这有用。
答
有一个nice example from Apple显示如何使用UIControl
来完成这种accessoryView自定义。
在UIControl中重写不是最简单的方法,但给你很多灵活性,以风格漂亮的配件视图。
如果我这样做,我注意到的UITableViewDelegate方法 - [的tableView:accessoryButtonTappedForRowWithIndexPath:]不再被调用。那是预期的行为?有没有办法保持这种方法的工作? – 2010-01-05 01:54:47
还有一些更多的解释在http://stackoverflow.com/questions/869421/using-a-custom-image-for-a-uitableviewcells-accessoryview-and-having-it-respond – jarnoan 2010-10-06 13:21:45
是否有可能使用UIApperance UITableViewCell上的代理? – Claus 2013-07-03 10:37:11