2009-08-05 2 views
4

테이블보기에서 확인 표시를 시도했지만 작동하지 않습니다. 해당 셀에서 다시 확인한 후 확인 표시를 적용하면 적용됩니다. 새로운 셀에는 적용되지 않습니다. 나를 돕는 사람은 누구입니까?객관적인 C를 사용하여 아이폰의 테이블보기에 체크 표시를 적용하는 방법?

thanks aamir.

내가

#pragma mark - 
#pragma mark Table Data Source Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section 
{ 
    return [self.chaptersList count]; 
} 

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{ 



static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease]; 
} 

NSUInteger row = [indexPath row]; 
NSUInteger oldRow = [lastIndexPath row]; 

cell.textLabel.text = [chaptersList objectAtIndex:row]; 
cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? 
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

return cell; 
} 

#pragma mark - 
#pragma mark Table Delegate Methods 
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    int row = [indexPath row]; 
    int oldRow = [lastIndexPath row]; 
    if (row != oldRow) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     lastIndexPath = indexPath; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
+0

를 사용하는 것을 잊지 말아. – Aamir

+0

코드가 실행될 때 처음부터 첫 번째 줄을 선택할 수 없습니다. 다른 줄을 선택하는 것이 좋습니다. –

+0

lastIndexPath를 선언하고 정의하는 위치 ???? –

답변

8

당신은 당신이 처음에 첫 선을 선택하면 확인 표시가 적용되지 않는 것을 의미합니까 다음 코드를 사용하고? 코드에 다른 문장을 추가 할 수는 있지만 코드를 실행할 때 시간이 낭비됩니다.

if (newRow != oldRow) 
{ 
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone; 

    lastIndexPath = indexPath; 
} 
else 
{ 
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    lastIndexPath = indexPath; 
} 
+0

다른 부분도 추가하지만 적용 할 수 없습니다. 체크 표시는 웹뷰에서 돌아와서 (과거) 셀을 다시 확인한 경우에만 나타납니다. – Aamir

+0

책의 소스 코드를 직접 실행 해보십시오. 직접 문제가 무엇인지 확인할 수 있습니다. –

+0

약간의 변경 올바른 작업을 위해 변경해야합니다 lastIndexPath = [indexPath retain]; – user930195

2

여기에 재미 버그

if (row != oldRow) 

이 때문에 oldRow가 있다면 전무 (또는 다른 단어에서 ... "0";-) 사용자 프레스 행 = "0" "if"문장은이 행이 체크되었다고 생각할 것입니다.

나는 다음과 같이 수정 : 그런데

- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    if (!self.lastIndexPath) { 
     self.lastIndexPath = indexPath; 
    } 

    if ([self.lastIndexPath row] != [indexPath row]) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     self.lastIndexPath = indexPath; 
    } 
    else { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

... 나는 그것을 일치, 내 코드는 위의 같은 생각하지만 잘 작동하지 않는 "자기"

+0

[lastIndexPath isEqual : indexPath]를 사용하여 – malhal

관련 문제