uitableview에서 스크롤 위치를 설정합니다
-
03-07-2019 - |
문제
iPhone의 연락처 응용 프로그램이 작동하는 방식과 유사한 응용 프로그램이 있습니다. 새 연락처 사용자를 추가하면 연락처 정보가있는보기 전용 화면으로 표시됩니다. 탐색 표시 줄에서 "모든 연락처"를 선택하면 사용자는 최근에 추가 된 연락처가보기에있는 모든 연락처 목록으로 탐색됩니다.
다음을 사용하여 뷰를 특정 행으로 이동할 수 있습니다.
[itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
...하지만 작동하지 않습니다. 나는 전화를 한 후 바로 이것을 호출하고있다 :
[tableView reloadData];
나는 전화 할 것이라고 생각하지 않는다고 생각합니다 selectRowAtIndexPath:animated:scrollPosition
여기서 방법. 그러나 여기에 없다면 어디에?
다음 방법에 따라 호출되는 대의원이 있습니까?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
해결책
비슷한 앱이 있다고 생각합니다. 항목 목록.
요약하면, 나는 넣습니다 reloadData
안에 viewWillAppear:animated:
그리고 scrollToRowAtIndexPath:...
안에 viewDidAppear:animated:
.
// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (dataHasChanged) {
self.dataHasChanged = NO;
[[self tableView] reloadData];
} else {
self.scrollToLast = NO; // No reload -> no need to scroll!
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (scrollToLast) {
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
이게 도움이 되길 바란다. 목록의 중간에 무언가를 추가하면 대신 해당 위치로 쉽게 스크롤 할 수 있습니다.
다른 팁
UitableView의 버튼 스크롤을 클릭하면 UP가 올라갈 때 귀하와 비슷한 종류의 내 응용 프로그램이 시도해 볼 수 있습니다.
[tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES];
10은 스크롤하려는 셀 수입니다.
이것이 도움이되기를 바랍니다 :-)
오프셋에서 설정되면 스크롤 애니메이션이 불가피합니다 viewDidAppear(_:)
. 초기 스크롤 오프셋을 설정하기에 더 좋은 장소는 다음과 같습니다 viewWillAppear(_:)
. 콘텐츠 크기가 해당 시점에서 정의되지 않기 때문에 테이블 뷰의 레이아웃을 강제해야합니다.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.setNeedsLayout()
tableView.layoutIfNeeded()
if let selectedIndexPath = selectedIndexPath, tableView.numberOfRows(inSection: selectedIndexPath.section) > 0 {
tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false)
}
}
스크롤을 테스트하기에 충분한 더미 접점이 추가되어 있습니까? TableView가 특정 크기 일 때만 iPhone이 스크롤 할 입력을 찾는 것 같습니다.
이것은 내 프로젝트에서 저에게 적합한 코드입니다. 이전 버튼을 사용하므로 테이블 뷰를 약간 더 아래로 스크롤하여 일반적으로 uitableviewscrollospition과 함께 진행됩니다. (이전 버튼은 이전 셀을 "볼 수 없다면"작동하지 않습니다.
사용자 정의 메소드 호출 중 일부를 무시하십시오.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//Show the navButtons
[self navButtonAnimation];
DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview;
self.parentController.lastCell = cell;
//Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible.
NSUInteger row = cell.cellPath.row;
NSUInteger section = cell.cellPath.section;
NSIndexPath *previousIndexPath = nil;
if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section
{
NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)};
previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];
}
else if (cell.cellPath.row != 0 && cell.cellPath.section != 0)
{
NSUInteger previousIndex[] = {section, row - 1};
previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];
}
[self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}