gpt4 book ai didi

ios - selectRowAtIndexPath 没有滚动到 UITableView 中的正确位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:25 25 4
gpt4 key购买 nike

我有大约 80 件元素。如果我选择的索引是 15(或更小的值),它会滚动到正确的位置。但如果选择索引为 70,则它不会滚动。但是当我手动滚动时它选择了该行。有谁知道如何解决这个问题?

-(void)select:(NSString*)selectedIndex {
if(selectedIndex == nil){
return;
}
UITableView *tableView = (UITableView*)view;
if(tableView == nil) {
return;
}

NSIndexPath *nextIndexPath = [self findIndexPath:selectedIndex table:tableView];

if(nextIndexPath != nil && ![viewController isSelectable:[selectedIndex intValue]]){
NSArray *indexPaths = [tableView indexPathsForVisibleRows];
nextIndexPath = [self nextMoveUp:nextIndexPath :indexPaths];
}
if(nextIndexPath != nil) {
[tableView selectRowAtIndexPath:nextIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
if ([tableView cellForRowAtIndexPath:nextIndexPath] == nil) {
// cell is not visible - scroll it into view
[tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
}
}

-(NSIndexPath*)findIndexPath:(NSString*)selectedIndex table:(UITableView*)tableView{
NSArray *indexPaths = [tableView indexPathsForVisibleRows];
if(indexPaths == nil || [indexPaths count] == 0) {
return nil;
}
NSIndexPath *lastIndexPath = NULL;
for (NSIndexPath *path in indexPaths) {
if(path.row == [selectedIndex intValue]){
return path;
}
lastIndexPath = path;
}
if(lastIndexPath != nil && lastIndexPath.row < [selectedIndex intValue]){
[tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
return [self findIndexPath:selectedIndex table:tableView];
}
return nil;
}

最佳答案

我刚遇到这个问题。在我的例子中,我在定义所有行之前调用 scrollToRowAtIndexPath(或 selectRowAtIndexPath),即在对所有行调用 cellForRowAtIndexPath 之前。我的解决方案是定义一个方法来选择行并使用 performSelectorOnMainThread 调用它以延迟直到第一次刷新完成。

关于ios - selectRowAtIndexPath 没有滚动到 UITableView 中的正确位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com