我是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方法調用圖像,這需要做出選擇。 –