【问题标题】:UITableViewCell accessoryView overlap custom button of cell OR how to change accessoryView position?UITableViewCell accessoryView 重叠单元格的自定义按钮或如何更改 accessoryView 位置?
【发布时间】:2016-01-13 19:37:28
【问题描述】:
我正在使用MSCellAccessory 为附件视图设置自定义颜色。它工作得很好。但是当我将按钮放在单元格中并单击单元格时,复选标记可见但它与删除按钮重叠。检查我的快照。
那么如何在 UITableViewCell 中改变 cell 的 accessoryView 位置或者在 delete 按钮下方设置 accessoryView。
【问题讨论】:
标签:
ios
objective-c
iphone
ipad
【解决方案1】:
如果你对 UITableViewCell 进行子类化,你可以在 layoutSubviews 中调整它
- (void)layoutSubviews {
[super layoutSubviews];
CGRect accessoryViewFrame = self.accessoryView.frame;
accessoryViewFrame.origin.x = CGRectGetWidth(self.bounds) - CGRectGetWidth(accessoryViewFrame);
self.accessoryView.frame = accessoryViewFrame;
}
否则......
添加子视图而不是附件视图
UIButton *indicatorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
indicatorBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[indicatorBtn setBackgroundImage:[UIImage imageNamed:@"right_indicator.png"]
forState:UIControlStateNormal];
indicatorBtn.backgroundColor = [UIColor clearColor];
//indicatorBtn.alpha = 0.5;
indicatorBtn.tag = indexPath.row;
[indicatorBtn addTarget:self action:@selector(method:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:indicatorBtn];
【解决方案2】:
我通过添加 UIImageView 来解决。放在删除按钮旁边,它对我有用。我没有使用表格单元配件。请检查以下代码。
- (UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier
{
CGRect cellRectangle = CGRectMake(0, 0, CGRectGetWidth(tableViewSavedSearch.frame), tableViewSavedSearch.rowHeight);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.frame = cellRectangle;
CGFloat imageWidth = 30;
//Name
UILabel *lblName = [DesignModel createPaddingLabel:CGRectMake(0, 0, CGRectGetWidth(cellRectangle) - (imageWidth * 2), tableViewSavedSearch.rowHeight) labelTag:TAG_SAVED_SEARCH_NAME textColor:COLOUR_BLUE_DARK textAlignment:NSTextAlignmentLeft textFont:[UIFont fontWithName:FONT_CENTURY_GOTHIC size:14] padding:UIEdgeInsetsMake(0, 5, 0, 5)];
[cell.contentView addSubview:lblName];
//Delete button
UIButton *button = [DesignModel createImageButton:CGRectMake(CGRectGetMaxX(lblName.frame), 0, imageWidth, tableViewSavedSearch.rowHeight) image:imageDelete tag:TAG_SAVED_SEARCH_DALETE];
[button addTarget:self action:@selector(btnSaveAndSearch_DeleteAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
//TICK
UIImageView *imageView = [DesignModel createImageView:CGRectMake(CGRectGetMaxX(button.frame), 0, imageWidth, tableViewSavedSearch.rowHeight) image:imageTick tag:TAG_SAVED_SEARCH_TICK contentMode:UIViewContentModeCenter];
[cell.contentView addSubview:imageView];
return cell;
}