2014-02-27 4 views
1

내 앱에서 사용자가 일부 설정을 검사 할 수 있지만 그 당시에 선택한 옵션 중 하나만 선택할 수있게하려는 설정보기가 있습니다. . 기본적으로 하나의 셀이 선택된 셀간에 체크 표시 토글

나는이 대답에 걸쳐 도움이 가지고 : iPhone :UITableView CellAccessory Checkmark

을하지만 내가 기본적으로 선택의 첫 번째 셀이있을 때 제대로 전환 얻을 수 아니에요.

이에 내 cellForRowAtIndexPath

if (indexPath.row == 0) 
{ 
    // Set the first cell checked by default 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else if([self.checkedIndexPath isEqual:indexPath]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

이것은 내 didSelectRowAtIndexPath

// Uncheck the previous checked row 
if(self.checkedIndexPath) 
{ 
    UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath]; 
    uncheckCell.accessoryType = UITableViewCellAccessoryNone; 
} 

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
self.checkedIndexPath = indexPath; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

내가 다시 확인 될 때까지 셀은 기본적으로 선택되어 있지만 다른 셀을 검사 할 때 첫 번째 셀이 여전히 선택되어 있고 그런 다음 다른 셀을 확인한 후 동작이 올바른 것입니다.

답변

1
if (!self.checkedIndexPath && indexPath.row == 0) 
{ 
    // save away what cell is already checked 
    self.checkedIndexPath = indexPath; 

    // Set the first cell checked by default 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
... 

대안 (! self.checkedIndexPath) 경우 self.checkedIndexPath = indexPath; 당신의 viewDidLoad에서

if([self.checkedIndexPath isEqual:indexPath]) 
{ 
    ... 
} 
else 
{ 
    ... 
} 

다른 추가 self.checkedIndexPath = [NSIndexPath indexPath forRow : 0 inSection : 0];

하고 방금 cellFor에서 checkedIndexPath = indexPath 검사가 ...

+0

self.checkedIndexPath = 추가 한 후 [0 inSection : NSIndexPath indexPathForRow 1]; 문제가 해결되었습니다. 감사합니다 @David – Oskariagon

+0

여기서 볼 수있는 것은 아마 inSection : 0, inSection : 1이 아니지만 전체 앱을 볼 수는 없습니다 :) –

+0

. self.checkedIndexPath를 [NSIndexPath indexPathForRow : 0 inSection : 1]로 설정하기 전에 self.checkedIndexPath를 로그 아웃 할 때 self.checkedIndexPath는 0입니다. 그러나 self.checkedIndexPath를 [NSIndexPath indexPathForRow : 0 inSection : 1]이고, self.checkedIndexPath는 -4611686018427387818입니다. 어떤 인덱스 경로가 선택되었는지에 따라 다른 설정을 지정하려는 경우이 문제가 발생합니까? 내 didSelectRowAtIndexPath? – Oskariagon