我是iOS新手,现在我需要执行与UITableView单元格相关的两个操作,在单元格中有一个检查图像,一旦单击对此我得到了一个触摸响应,但是如果我点击了图像区域以外的任何地方didSelectRowAtIndexPath方法正在调用。如何禁用UITableView单元格中特定部分的tableViewCell选择iOS
我应该怎么做才能实现仅对单元格内的选定区域调用didSelectRowAtIndexPath
并忽略剩余区域,或禁用单元格的几个区域内的触摸。
我是iOS新手,现在我需要执行与UITableView单元格相关的两个操作,在单元格中有一个检查图像,一旦单击对此我得到了一个触摸响应,但是如果我点击了图像区域以外的任何地方didSelectRowAtIndexPath方法正在调用。如何禁用UITableView单元格中特定部分的tableViewCell选择iOS
我应该怎么做才能实现仅对单元格内的选定区域调用didSelectRowAtIndexPath
并忽略剩余区域,或禁用单元格的几个区域内的触摸。
我明白你的问题。您必须覆盖您的自定义UITableViewCell中的touchesBegan(触及:Set,withEvent事件:UIEvent?)方法。
class CustomTableViewCell: UITableViewCell {
var isValidTouch: Bool = true
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let point = touch.locationInView(self.contentView)
let validTouchPoint = self.contentView.bounds.width - (checkMarkView.bounds.width + 16.0) //Trailing + Leading space
if point.x < validTouchPoint {
isValidTouch = true
} else {
isValidTouch = false
//Send tap gesture callback to your viewcontroller
}
}
super.touchesBegan(touches, withEvent: event)
}
}
在你的viewcontroller UITableViewDelegate方法中检查你的触摸是否有效。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
if cell.isValidTouch {
//do whatever
}
}
请的cellForRowAtIndexPath写这样的代码:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
如果添加复选框,取消选中框,然后写代码的cellForRowAtIndexPath
if(indexPath.row == 0){
UIButton *btnForCheckAC = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.size.width - 50 , 10,15,15)];
btnForCheckAC.tag = BUSTYPEBTN_TAG;
UIImage *image = (isAcButtonClkd)?[UIImage imageNamed:@"check-mark-pink.png"]:[UIImage imageNamed:@"uncheckbox.png"];
[btnForCheckAC addTarget:self action:@selector(busTypebtnAction:event:) forControlEvents:UIControlEventTouchUpInside];
[btnForCheckAC setImage:image forState:UIControlStateNormal];
cell.accessoryView = btnForCheckAC;
}
然后在didSelectedRowAt IndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
isAcButtonClkd = !isAcButtonClkd;
}
}
写按钮动作:
-(void)busTypebtnAction:(id *)sender event:(id)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tblBookBus];
NSIndexPath *indexPath = [self.tblBookBus indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tblBookBus didSelectRowAtIndexPath: indexPath];
}
}
更改根据您的要求的名称。
我希望这对你有帮助。
如果确认图像的UIButton那就试试这个
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsSelection = false;
}
希望它会帮助你
设置的tableview选择风格为UITableViewCellSelectionStyleNone
实施下列任何委托方法,这会阻止didSelectRowAtIndexPath
被呼叫。
设置userInteractionEnabled
为您检查的ImageView和实施(与TapGesture使用UIButton
或UIImageView
)
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
OR
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
希望这对图像自来水的方法帮助。
首先作出这样的ImageView作为的UIButton
,并cellselectionstyle
为无。
在cellForRowIndexPath:
{
[cell.CloseButton addTarget:self action:@selector(CrossClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.CloseButton setTag:indexPath.row];
}
的方法采取点击按钮行号标记值就像这样:
-(void)CrossClicked:(UIButton *)sender{
int index = sender.tag;
//on basis of index you can perform all task
}
你可以给你一个tableviewcell的截图吗? –
禁用触摸的区域与您引用的图像分开吗?所以单元格有3个不同的区域:1)触发器didSelectRowAtIndexPath,2)检查你的图像,3)不响应触摸? – dylansturg
@dylansturg:是的..!不是三个,而是有两个区域,1.直到检查图像,然后2.检查图像区域。问题是,如果我只是稍微点击图像didSelectRowAtIndexPath方法调用图像,这需要做出选择。 –