2014-04-27 2 views
0

내 코드에서 사용자가 목록에 새 항목을 추가하면 해당 항목을 항목 배열에 추가하고 정렬 한 다음 [tableView reloadData]을 호출합니다. 목록을 정렬하기 때문에 때로는 새로 추가 된 항목이 보이지 않게됩니다. 나는 그것이 보이지 않을 때 새로 추가 된 아이템의 UITableViewCell을 얻고보기로 다시 스크롤 할 것입니다. cellForRowAtIndexPath에서보기에서 UITableViewCell을 가져와보기로 스크롤하는 방법

나는 각 셀의 태그 기록을 추가

cell.tag = listItems.recordID; 

방법 addNewItem에서이 항목을 새로운 항목을 추가 및 정렬 후 :

for (int i = 0; i<listItems.count; i++) 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    int tag = cell.tag; 
    if (tag == newItemRecordID) 
     [self.tableView scrollToRowAtIndexPath:indexPath 
           atScrollPosition:UITableViewScrollPositionMiddle 
             animated:YES]; 
} 

셀은보기 tag를 벗어나면 속성은 0을 반환하며 셀을 가져올 수 없습니다.

어떻게 그 세포를 얻을 수 있습니까?

cell.tag = indexPath.row; 

세포 태그를 설정 한 후, 당신이 호출 할 수있는 함수 :

for (int i = 0; i<listItems.count; i++) 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    int tag = cell.tag; 
    if (tag == newItemRecordID) 
     [self.tableView scrollToRowAtIndexPath:indexPath 
           atScrollPosition:UITableViewScrollPositionMiddle 
             animated:YES]; 
} 
+0

'cellForRowAtIndexPath :'현재 보이지 않는 셀에 대해 nil을 반환하므로 기본 알고리즘은 다음과 같습니다. 불완전한. nil에 전송 된 모든 메소드는 0을 반환하므로 태그는 현재 보이지 않는 셀에 대해서는 0이됩니다. Lebyrt의 해결책이 효과가 있습니다. –

답변

1

는 것 같아요, 같은 것을 사용하는 것이 좋을 것이다

0

다음과 같이 cellForRowAtIndexPath 세트 세포 태그에 :

for (int i = 0; i < listItems.count; i++) 
{ 
    // Replace 'ListItem' with the name of your class. 
    ListItem *listItem = [listItems objectAtIndex:i]; 
    if (listItem.recordID == newItemRecordID) 
    { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
     [self.tableView scrollToRowAtIndexPath:indexPath 
           atScrollPosition:UITableViewScrollPositionMiddle 
             animated:YES]; 
     break; 
    } 
} 
+0

6 개의 셀이 끝나면 cell.tag는 0을 반환합니다. 참고 6 개의 셀이 뷰에 표시됩니다. – Hadu

관련 문제