didSelectRowAtIndexPath : 호출되지 않음
나는이 UITableView내의 하위 뷰로 UIScrollVIew내에 의해 제어되는 기본보기입니다 MainViewController.
MainViewController.h에서
@interface MainViewController : UIViewController <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>
// other stuff here...
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
MainViewController.m에서
@synthesize myTableView;
// other stuff here...
- (void)viewDidLoad {
myTableView.delegate = self;
myTableView.datasource = self;
}
// other stuff here...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"listAttributesSegue" sender:self];
}
didSelectRowAtIndexPath메서드 자체와 그 안에있는 코드 줄 모두에 중단 점을 설정했고 둘 다 호출되지 않았기 때문에 호출되지 않는다는 것을 알고 있습니다. 또한 런타임에 셀을 수정하는 다른 기능이 있고 완벽하게 잘 작동하기 때문에 데이터 소스가 올바르게 작동하고 있음을 알고 있습니다. iOS 5.0이 개발 대상으로 설정된 최신 Xcode를 사용하고 있습니다. 나는 답을 찾아서 찾았다. 누구나 아이디어가 있습니까?
편집 : 답을 찾았습니다. 나는했다 UITapGestureRecognizermyTableView의 수퍼위한 설정입니다. 이것은 선택 호출을 무시합니다. 그게 될 수 있다고 제안한 사람에 대한 크레딧. 정답으로 표시하기 전에 답변이 삭제되었습니다.
편집 2 : 많은 사람들이 이것에 대해 언급하고 있으므로 나는 그것을 공유하고 싶습니다. 이 문제가 발생하는 경우로 설정 myGestureRecognizer.cancelsTouchInView하면 false모든 것이 정상적으로 작동합니다.
답을 찾았습니다. myTableView의 superView에 UITapGestureRecognizer를 설정했습니다. 이것은 선택 호출을 무시합니다. 그게 될 수 있다고 제안한 사람에 대한 크레딧. 정답으로 표시하기 전에 답변이 삭제되었습니다.
테이블보기가 이벤트를 가로 챌 수 있도록 제스처 인식기 에서 cancelsTouchesInView속성을로 설정합니다 NO.
Swift 3 용으로 업데이트되었습니다.
코드에서 UITapGestureRecognizer를 사용하는 경우 :-# Swift 3 아래 코드 줄을 사용합니다.
extension YourViewController{
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
tap.cancelsTouchesInView = false
}
func dismissKeyboard() {
view.endEditing(true)
}
}
호출 방법 : -In ViewDidLoad ()
self.hideKeyboardWhenTappedAround()
문제는 대소 문자를 구분합니다. 코드 :
- (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
해야한다
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
동일한 이름으로 tableview에 대한 인스턴스 변수를 정의 했습니까? 그렇지 않다면 이것이 문제가 될 수 있습니다.
_myTableView.delegate = self;
_myTableView.datasource = self;
또는-
self.myTableView.delegate = self;
self.myTableView.datasource = self;
결국 오타 일 수도 있습니다. 함수가 아님을 확인 didDeselectRowAtIndexPath:( 드 선택이 아닌 선택).
내 솔루션은 다음과 같습니다.
설정
cancelsTouchesInView하려면No어떤 tapGesture의내 사용자 정의 셀에서
userInteractionEnable으로 설정되어NO있으며 간단히 삭제userInteractionEnable = No하고 문제가 해결되었습니다.
필요한 것을 제외한 다른보기 터치를 취소합니다.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
if (touch.view == your view) {
return YES;
}
return NO;
}
죄송합니다. 댓글을 추가 할 포인트가 충분하지 않습니다. Garret의 답변은 훌륭하지만 추가하겠습니다.
You can still have your gesture recognizer but you will need to set 'Cancels touches in view' to NO - then the gestures will be handed on to the view and your UITableView will work fine.
After trying many, many approaches this seems to be the correct way of doing things: a tap gesture recognizer with 'cancel touches in view' is like having an invisible layer on top of everything that grabs all the events and routes them to the view controller (the proxy). The view controller then looks at the gesture to see if it has an action binding (buttons etc.) and will route those and any remaining will just go to the gesture handler. When using a UITableView it is expecting to receive the tap but the view controller snaffles it when you have 'Cancels touches in view'.
I was having this issue for a while, and I did not see any reference to it here, so for reference, another reason for this could be that:
tableView.editing = YES;
but
tableView.allowsSelectionDuringEditing = NO;
As per documentation for
- tableView:didSelectRowAtIndexPath:
This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "Managing Selections" in Table View Programming Guide for iOS for further information (and code examples) related to this method.
My case is strange. My tableView has 2 sections. 1st section's cells work fine about tableView:didSelectRowAt:, but 2nd section's cells doesn't trigger didSelectRowAt:.
The above problem happens at iPhone 4s, iOS 9.3. But in iPhone 5s, iOS 10.3, there are no problems, those cells works fine. It seems like iOS 9 bugs about UITableView.
After many tests, I found out one line codes produces this bug.
tableView.estimatedSectionHeaderHeight = 60.0
Because the 2nd sections has no header view. I remove this line, and all works fine.
A cell can be selected by the user (tapping on the row), by calling "tableView.selectRowAtIndexPath(..)" or "cell.setSelected(true, ...).
If the cell is selected by calling "cell.setSelected(true)", the user cannot deselect the cell anymore.
If the cell is selected by calling "tableView.selectRowAtIndexPath()", the user can deselect the cell as expected.
I had intermittent failure of didSelectRowAtIndexPath: being called on my custom cell press.
I discovered that if I stopped calling [tableView reloadData] very often (10 Hz), and changed it to update every 2 seconds, almost every press would successfully call didSelectRowAtIndexPath:
It seems like reloading the view blocks presses.
It's work for me, can you try!
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap)
참고URL : https://stackoverflow.com/questions/8952688/didselectrowatindexpath-not-being-called
'Program Club' 카테고리의 다른 글
| Mongoose 및 NodeJS 프로젝트의 파일 구조 (0) | 2020.12.14 |
|---|---|
| Linux 명령 줄 : du — 각 디렉토리에 대한 총계 만 표시하는 방법 (0) | 2020.12.14 |
| Swift에서 UITextField / UIView에 대한 애니메이션 흔들기 (0) | 2020.12.13 |
| Objective-C 빌드의 중복 기호 오류? (0) | 2020.12.13 |
| LINQ는 느리기 때문에 피해야합니까? (0) | 2020.12.13 |