prosource

프로그래밍 방식으로 테이블 보기 행 선택

probook 2023. 5. 3. 21:32
반응형

프로그래밍 방식으로 테이블 보기 행 선택

프로그래밍 방식으로 선택하는 방법UITableView하도록 노를 젓다

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath

처형당했나요? selectRowAtIndexPath행만 강조 표시됩니다.

자누스가 말했듯이,

이 메서드를 호출해도(-selectRowAtIndexPath: animated: scrollPosition:) 딜러가 tableView: willSelectRowAtIndexPath: 또는 tableView: didSelectRowAtIndexPath: 메시지를 수신하지 않으며 UITableView도 전송하지 않습니다.SelectionDidChangeNotification을 관찰자에게 통지합니다.

그래서 당신은 그냥 전화하면 됩니다.delegate스스로 방법을 생각해 보세요.

예:

Swift 3 버전:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)

목표 C 버전:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath 
                            animated:YES 
                      scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

Swift 2.3 버전:

 let indexPath = NSIndexPath(forRow: 0, inSection: 0);
 self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
 self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)

참조 문서에서:

이 메서드를 호출해도 딜러가 다음을 수신하지 않습니다.tableView:willSelectRowAtIndexPath:또는tableView:didSelectRowAtIndexPath:메시지, 메시지도 보내지 않습니다.UITableViewSelectionDidChangeNotification참관인에 대한 통지

제가 해야 할 일은 다음과 같습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self doSomethingWithRowAtIndexPath:indexPath];
}

그런 다음 호출할 위치에서 RowAt를 선택합니다.IndexPath 대신 doSomethingWithRowAt를 호출합니다.인덱스 경로.또한 selectRowAt를 추가로 호출할 수 있습니다.UI 피드백을 수행하려면 IndexPath를 선택합니다.

UITableView의 selectRowAtIndexPath: animated: scrollPosition: 이 기능을 수행해야 합니다.

그냥 지나침UITableViewScrollPositionNonescrollPosition의 경우 사용자는 아무런 움직임도 볼 수 없습니다.


또한 다음 작업을 수동으로 실행할 수 있어야 합니다.

[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]

먼저 가세요.selectRowAtIndexPath:animated:scrollPosition:연관된 논리와 마찬가지로 하이라이트가 발생합니다.

Swift 3/4/5 솔루션

행 선택

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)

만약 당신이 어떤 행을 선택하고 싶다면, 이것은 당신에게 도움이 될 것입니다.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[someTableView selectRowAtIndexPath:indexPath 
                           animated:NO 
                     scrollPosition:UITableViewScrollPositionNone];

행도 강조 표시됩니다.그러면 위임

 [someTableView.delegate someTableView didSelectRowAtIndexPath:indexPath];

iPad와 iPhone 플랫폼에는 두 가지 방법이 있으므로 두 가지를 모두 구현해야 합니다.

  • 선택 핸들러 및
  • 세그

    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];
    

이 범주를 사용하여 테이블 행을 선택하고 지연 후 지정된 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

언급URL : https://stackoverflow.com/questions/2035061/select-tableview-row-programmatically

반응형