프로그래밍 방식으로 테이블 뷰 행 선택
프로그래밍 방식으로 UITableView
행을 선택하여 어떻게
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
처형? selectRowAtIndexPath
행만 강조 표시합니다.
에서 참조 문서 :
이 메서드를 호출해도 대리인이
tableView:willSelectRowAtIndexPath:
또는tableView:didSelectRowAtIndexPath:
메시지를 받거나UITableViewSelectionDidChangeNotification
관찰자 에게 알림을 보내지 않습니다 .
내가 할 일은 :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self doSomethingWithRowAtIndexPath:indexPath];
}
그런 다음 selectRowAtIndexPath를 호출하려는 위치에서 대신 doSomethingWithRowAtIndexPath를 호출하십시오. 또한 UI 피드백이 발생하도록하려면 selectRowAtIndexPath를 추가로 호출 할 수도 있습니다.
Jaanus가 말한 것처럼 :
이 (-selectRowAtIndexPath : animated : scrollPosition :) 메서드를 호출해도 대리자가 tableView : willSelectRowAtIndexPath : 또는 tableView : didSelectRowAtIndexPath : 메시지를 받거나 UITableViewSelectionDidChangeNotification 알림을 관찰자에게 보내지 않습니다.
따라서 delegate
메소드를 직접 호출하면 됩니다.
예를 들면 다음과 같습니다.
스위프트 3 버전 :
let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)
ObjectiveC 버전 :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
스위프트 2.3 버전 :
let indexPath = NSIndexPath(forRow: 0, inSection: 0);
self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
UITableView의 selectRowAtIndexPath : animated : scrollPosition : 트릭을 수행해야합니다.
UITableViewScrollPositionNone
scrollPosition을 전달 하면 사용자가 움직임을 볼 수 없습니다.
작업을 수동으로 실행할 수도 있습니다.
[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]
그런 다음 selectRowAtIndexPath:animated:scrollPosition:
강조 표시와 관련 논리가 발생합니다.
일부 행을 선택하려면 도움이 될 것입니다.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[someTableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
이것은 또한 행을 강조 표시합니다. 그런 다음 위임
[someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];
스위프트 3.0 솔루션
행 선택
let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)
행 선택 해제
let deselectIndexPath = IndexPath(row: 7, section: 0)
tblView.deselectRow(at: deselectIndexPath, animated: true)
tblView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)
이 범주를 사용하여 테이블 행을 선택하고 지연 후 지정된 segue를 실행하십시오. 메소드
내에서 이것을 호출하십시오 viewDidAppear
.
[tableViewController delayedSelection:withSegueIdentifier:]
@implementation UITableViewController (TLUtils)
-(void)delayedSelection:(NSIndexPath *)idxPath withSegueIdentifier:(NSString *)segueID {
if (!idxPath) idxPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self performSelector:@selector(selectIndexPath:) withObject:@{@"NSIndexPath": idxPath, @"UIStoryboardSegue": segueID } afterDelay:0];
}
-(void)selectIndexPath:(NSDictionary *)args {
NSIndexPath *idxPath = args[@"NSIndexPath"];
[self.tableView selectRowAtIndexPath:idxPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:idxPath];
[self performSegueWithIdentifier:args[@"UIStoryboardSegue"] sender:self];
}
@end
iPad 및 iPhone 플랫폼에는 두 가지 방법이 있으므로 두 가지를 모두 구현해야합니다.
- 선택 핸들러
segue.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; // Selection handler (for horizontal iPad) [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; // Segue (for iPhone and vertical iPad) [self performSegueWithIdentifier:"showDetail" sender:self];
참고 URL : https://stackoverflow.com/questions/2035061/select-tableview-row-programmatically
'programing tip' 카테고리의 다른 글
'sudo gem install'또는 'gem install'및 gem 위치 (0) | 2020.06.27 |
---|---|
C # 빈 문자열 배열 선언 (0) | 2020.06.27 |
파이썬 스크립트에서 현재 자식 해시를 얻으십시오 (0) | 2020.06.27 |
CSS를 사용하여 배경 이미지 중심 맞추기 (0) | 2020.06.27 |
XOR이 해시를 결합하는 기본 방법 인 이유는 무엇입니까? (0) | 2020.06.27 |