Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何允许屏幕外UITableViewCell下推内容偏移?_Ios_Iphone_Uitableview - Fatal编程技术网

Ios 如何允许屏幕外UITableViewCell下推内容偏移?

Ios 如何允许屏幕外UITableViewCell下推内容偏移?,ios,iphone,uitableview,Ios,Iphone,Uitableview,我有一个UITableView,它包含一堆单元格 如果用户单击第二个单元格,则第一个单元格应设置动画并显著扩展其高度,并向下推所有其他单元格,使用户的滚动位置保持不变。当两个单元格都在屏幕上时,我的代码100%正常工作。UITableView的contentSize会显著增长,contentOffset不会更改 但是如果用户向下滚动,只有第二个单元格可见,当他们点击它时,第一个单元格会在屏幕外展开,用户不会发生任何事情 UITableView的contentSize不会更改,contentOff

我有一个UITableView,它包含一堆单元格

如果用户单击第二个单元格,则第一个单元格应设置动画并显著扩展其高度,并向下推所有其他单元格,使用户的滚动位置保持不变。当两个单元格都在屏幕上时,我的代码100%正常工作。UITableView的contentSize会显著增长,contentOffset不会更改

但是如果用户向下滚动,只有第二个单元格可见,当他们点击它时,第一个单元格会在屏幕外展开,用户不会发生任何事情

UITableView的contentSize不会更改,contentOffset也不会更改。一旦用户稍微向上滚动,看到扩展单元格的底部,contentSize和contentOffset就会更新,以反映第一个单元格确实要大得多的事实(但从用户的角度来看,没有任何变化)

为扩展前后单元格的索引路径调用
heightforrowatinexpath
,将返回预期值

我的代码有很多内容,但是应该为扩展设置动画的主要部分在这里:

UIView animateWithDuration:0.3
 animations:^{
   performSelectorOnMainThread(@selector(updateItemHeights:), withObject: nil, waitUntilDone: YES)
 }
 completion:^(BOOL finished){}]
以及updateItemHeights的实施:

   beginUpdates
   endUpdates
   self.contentSize = sizeThatFits([contentSize.width, CGFLOAT_MAX])
似乎iOS正试图通过允许上面的单元格扩展来保持用户在当前环境中


如何让屏幕外的单元格向下推其他单元格?

多亏了表视图出列系统,当一个单元格不可见时,它不会被加载。因此,如果表格视图在屏幕上不可见,它将不会设置更改的动画

我在这里看到两个选项:

在更新其高度之前滚动到动画单元格

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let animatedIndexPath = ...
    let visibleRows = tableView.indexPathsForVisibleRows ?? []
    if visibleRows.contains(animatedIndexPath) {
        self.tableView.reloadRows(at: [animatedIndexPath], with: .automatic)
    } else {
        UIView.animate(withDuration: 0.3, animations: {
            self.tableView.scrollToRow(at: animatedIndexPath, at: .none, animated: false)
        }) { _ in
            self.tableView.reloadRows(at: [animatedIndexPath], with: .automatic)
        }
    }
}
更新单元格后调整内容偏移量

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let animatedIndexPath = ...
    let visibleRows = tableView.indexPathsForVisibleRows ?? []
    if visibleRows.contains(animatedIndexPath) {
        self.tableView.reloadRows(at: [animatedIndexPath], with: .automatic)
    } else {
        let offset = tableView.contentOffset
        tableView.reloadData()
        tableView.layoutIfNeeded() // forces the new offset computation
        tableView.setContentOffset(offset, animated: true)
    }
}
(由于表视图动态高度计算,您可能会遇到一些问题,请禁用它
tableView.estimatedRowHeight=0