Nice programing

선택한 UItableViewCell이 선택되면 파란색으로 유지됨

nicepro 2020. 12. 31. 23:28
반응형

선택한 UItableViewCell이 선택되면 파란색으로 유지됨


사용자가 UITableView 행을 선택한 후 뷰를 푸시하면 행이 파란색으로 강조 표시되고 새 뷰가 나타납니다. 괜찮아. 그러나 내가 '뒤로'이동하면 행이 여전히 파란색으로 강조 표시됩니다. 다음은 내 didSelectRowAtIndexPath 코드입니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil; 
}

내가 도대체 ​​뭘 잘못하고있는 겁니까?


위의 답변에서 지적했듯이 행을 명시 적으로 선택 취소해야합니다. 이를 수행하는 방법에 대해 두 가지 옵션이 있습니다. 첫 번째는 선택 직후 행을 선택 취소하는 것입니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

그것은 잘 작동하지만 대안 UITableViewController이 있으며 행을 선택한 상태로 두었다가 뷰가 다시 나타날 때 선택 해제하는 방법이 있습니다 (누르는 컨트롤러가 스택에서 튀어 나온 후).

이것은 사용자가 돌아올 때 이전 선택을 엿볼 수 있다는 약간의 이점이 있으므로 이전에 선택한 것을 볼 수 있습니다.

이를 구현하려면 다음을 재정의하면됩니다 viewWillAppear.

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

내가 말했듯이, 이것은 구현의 기본 무엇 UITableViewController의는 viewWillAppear:당신이 사용하는 경우 그렇게 UITableViewController하고 있지 이 동작을보고, 당신이 호출되어 있는지 확인해야합니다 super자신의 클래스에서 구현 ' viewDidAppear:.

업데이트 (2013 년 10 월 30 일) : 글쎄, 이것은 인기있는 답변입니다! 벤 바르게 의견에서 지적 하듯이,있는 UITableViewController는 사실에서이 작업을 수행 viewWillAppear:하지 viewDidAppear:-이 정확한 타이밍이다. 또한 clearsSelectionOnViewWillAppearUITableViewController 속성을 사용하여이 동작을 켜고 끕니다 . 나는 이것을 반영하기 위해 위의 대답을 수정했습니다.


UITableViewControllerclearsSelectionOnViewWillAppear원하는 것을 정확히 수행 하는 BOOL 속성 이 있습니다.

기본적으로로 설정되어 YES있지만 자신의 viewWillAppear:메서드 를 구현하여이 속성을 (때로는 실수로) 비활성화 할 수 있습니다 . 나는 이것이 [UITableViewController viewWillAppear:]당신이 그것을 재정의하면 결코 호출되지 않을 수 있는 선택 취소가 발생하기 때문이라고 생각 합니다.

해결책은 간단합니다. viewWillAppear:해당 메서드 의 버전 어딘가에서 super의 버전을 호출 하십시오.

- (void)viewWillAppear:(BOOL)animated {
  // Your custom code.
  [super viewWillAppear:animated];
}

Apple은 view {Did, Will} {A, Disa} ppear 메서드를 재정의하는 경우 항상 슈퍼 버전을 호출 할 것을 권장합니다.

참고 문헌


선택을 취소해야합니다.

[tableView deselectRowAtIndexPath:indexPath animated:YES];


컨트롤러가를 기반으로하는 UITableViewController경우이 기능을 무료로 사용할 수 있습니다. 그러나 나는 종종 UIViewControllerno로 사용 했습니다. 이외에 다른 컨트롤 UITableView,이 경우에, 당신은 당신의 오버라이드 (override) viewWillAppear에을

-(void) viewWillAppear:(BOOL)animated{
    // Unselect the selected row if any
    NSIndexPath*    selection = [devListTableview indexPathForSelectedRow];
    if (selection){
        [tableview deselectRowAtIndexPath:selection animated:YES];
    }
}

[tableView deselectRowAtIndexPath : indexPath animated : YES] 만 호출하면됩니다.


The default behaviour of UITableViewController deselects the row when the user returns from the detail view. The response given by Luke is fine, but I want to point out the reason for it:

1- If you have your UITableViewController like it was when you created it from scratch, you will have all the default behaviours.

2- If in the situation 1 you add a -viewWillAppear or -viewDidAppear, then you will be overwriting the standard behaviour. Them, if you want that the row deselects on return, you must say the super explicitly that you'd like it to deselect the row by himself as it always did! To achieve this, as Luke says, you must call [super viewWillAppear:animated] or [super viewDidAppear:animated] like this:

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // Here goes all of your stuff
}

Another solution is to call UITableView's reloadData in viewWillAppear

ReferenceURL : https://stackoverflow.com/questions/2803061/selected-uitableviewcell-staying-blue-when-selected

반응형