2014-04-10 2 views
0

나는 많은 셀을 가지고 있는데, 셀에는 일련의 행을 표시하는 레이블이 있습니다. 테이블에서 셀을 움직이면 셀이 새로운 행에 따라 번호를 다시 매기기를 원합니다. 아래 코드를 시도한 후에 숫자는 모두 꺼져 있습니다. 어떻게 해결할 수 있을까요?이동 한 후 셀 번호를 다시 매기기

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ 



    for (int section = 0; section < [tableView numberOfSections]; section++) { 
     for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) { 
      NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section]; 
      CustomCell* cell = (CustomCell *)[tableView cellForRowAtIndexPath:cellPath]; 

      NSString *rowString1 = [NSString stringWithFormat:@"%d.", row + 1]; 
      cell.periodLabel.text = rowString1; 
     } 
    } 
} 

답변

0

Model-View-Controller를 친구로 만들어보세요.

"셀 번호 다시 매기기"를 생각하는 대신 "개체의 번호를 다시 매기십시오"라고 생각하십시오. Model 객체 (예 : MyNumberedRow)를 사용하여 셀의 데이터를 나타냅니다. 셀이 이동하면 해당 셀의 MyNumberedRow 인스턴스를 검색하고 업데이트하십시오.

논리적 테이블에 수백 개의 항목이 있더라도 CustomCell 인스턴스로 인식되는 항목 만 표시됩니다. 화면에 없으면 셀은 존재하지 않습니다.

사이드 노트 : 이 아니라 int을 사용하십시오. 32/64 비트 변환시 타입 문제를 방지합니다. 반환 유형은 -numberOfSections입니다.

관련 문제