2013-08-11 1 views
0

내 UITableViewCell의 accessoryView로 UIButton이 있습니다. cellForRowAtIndexPath에서 블록을 사용하여 백그라운드 서버에서 데이터를 가져옵니다. 블록이 호출되면 검색된 데이터에 따라 셀의 단추에 대한 selected 속성을 설정합니다.서버에서 데이터를 가져온 후 백그라운드에서 올바른 UITableViewCell을 업데이트하십시오.

사용자가 TableView를 빠르게 위아래로 스크롤 할 때 일부 단추 상태가 잘못 설정되는 경우가 있습니다. 나는 틀린 UITableViewCell이 블록 내부에서 사용되고 있기 때문에 (이것이 현재 아마 오프 스크린 인 실제 셀 대신에 재사용 된 셀이라고 생각한다).

블록 안의 셀이 내가 원하는 셀인지 어떻게 확인합니까? 셀이 올바른 세포가 아닌 경우

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

    //FoundUserCell is a subclass of UITableViewCell 
    FoundUserCell *cell = [tableView dequeueReusableCellWithIdentifier:[self cellIdentifierTableView:tableView]];  

    //set the textLabel (this works already) 
    cell.textLabel = @"something"; 

    //fetch info from the DB that I need to state the state of the button 
    //theButton is a property of FoundUserCell 
    //theButton is always set to a FoundUserCell's accessoryView 
    [self fetchInBackgroundWithBlock:^(int buttonState) { 

     //I think that, at this point, cell is sometimes no longer the cell I want    

     if(buttonState == 0) 
      cell.theButton.selected = NO; 
     else 
      cell.theButton.selected = YES; 

     //WHAT CAN I DO INSIDE THIS BLOCK TO MAKE SURE THAT THE CELL I'M SETTING IS THE CELL I WANT? 
    }]; 

    return cell; 
} 

는 기본적으로,이 블록 내부의 버튼의 상태를 설정하지 않으 :

여기에 중요한 부분이 포함되어 내 코드의 단순화 된 버전입니다.

답변

1

그래, 셀은 블록의 오른쪽 셀을 받아야하므로, (변경) 다시 사용됩니다

[self fetchInBackgroundWithBlock:^(int buttonState) { 
     UITableViewCell *rightCell = [tableView cellForRowAtIndexPath:indexPath]; 
     // code with rightCell 
}]; 
관련 문제