2013-01-19 4 views
0

행을 이동 한 후 셀과 관련된 비즈의 lineandPin 번호를 변경했습니다. cellForRowAtIndexpath가 다시 호출되면 일이 정렬됩니다. 행을 이동 한 후 cellForRowAtIndexPath를 다시 호출하는 방법은 무엇입니까?

enter image description here

내가 그것을 잘하고있는 중이 야 확실하지 오전 내 코드

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 
    NSMutableArray * mutableBusinessBookmarked= self.businessesBookmarked.mutableCopy; 
    Business *bizToMove = mutableBusinessBookmarked[sourceIndexPath.row]; 
    [mutableBusinessBookmarked removeObjectAtIndex:sourceIndexPath.row]; 
    [mutableBusinessBookmarked insertObject:bizToMove atIndex:destinationIndexPath.row]; 
    self.businessesBookmarked=mutableBusinessBookmarked; 
    [self rearrangePin]; 
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]; 
    [self.table reloadData]; 
} 
  1. 입니다. 데이터 모델을 업데이트하고 전화를 걸었습니다. moveRowAtIndexPath
  2. [tableView moveRowAtIndexPath... 아무 것도 보이지 않습니다. 행이 호출되는지 여부에 관계없이 행이 이동됩니다.
  3. self.table reloadData를 호출하는 것이 현명하다고 생각하지 않습니다. 그러나 왼쪽의 번호를 업데이트하고 싶습니다. cellForRowAtindexpathself.table reloadData에도 불구하고 호출되지 않습니다.

답변

3

셀 구성 논리를 별도의 방법으로 이동하는 것이 좋습니다. 그런 다음 moveRowAtIndexPath에서이 메서드를 직접 호출하여 보이는 셀을 업데이트 할 수 있습니다.

- (void)configureCell:(UITableViewCell *)cell 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    // Get data for index path and use it to update cell's configuration. 
} 

- (void)reconfigureVisibleCells 
{ 
    for (UITableViewCell *cell in self.tableView.visibleCells) { 
     [self configureCell:cell]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"]; 
    [self configureCell:cell]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 
    // Update data model. Don't call moveRowAtIndexPath. 
    [self reconfigureVisibleCells]; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self configureCell:cell]; 
} 

몇 가지 추가 의견 :

  1. cellForRowAtIndexPath 만 테이블 뷰가 새로운 셀을 표시해야 할 때 호출되는 예를 들어. 눈에 보이는 세포는 절대로 호출되지 않습니다.
  2. 데이터 모델이 변경되어 해당 변경 사항을 UI에 전파해야하는 경우 moveRowAtIndexpath으로 전화하는 것이 좋습니다. 귀하의 경우는 이것의 반대입니다. 즉 UI가 데이터 모델에 변경 사항을 전파하고 있습니다. 따라서 moveRowAtIndexPath으로 전화하지 않으실 것입니다.
  3. cellForRowAtIndexPath 이후에 테이블 뷰가 사용자 지정 내용을 덮어 쓰는 경우가 있기 때문에 항상 willDisplayCell에 셀을 다시 구성합니다.