내 코드에서 사용자가 목록에 새 항목을 추가하면 해당 항목을 항목 배열에 추가하고 정렬 한 다음 [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];
}
'cellForRowAtIndexPath :'현재 보이지 않는 셀에 대해 nil을 반환하므로 기본 알고리즘은 다음과 같습니다. 불완전한. nil에 전송 된 모든 메소드는 0을 반환하므로 태그는 현재 보이지 않는 셀에 대해서는 0이됩니다. Lebyrt의 해결책이 효과가 있습니다. –