Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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 top到UITableView top_Ios_Swift_Uitableview_Uiscrollview - Fatal编程技术网

Ios 加载视图控制器时如何滚动到特定的UITableViewCell top到UITableView top

Ios 加载视图控制器时如何滚动到特定的UITableViewCell top到UITableView top,ios,swift,uitableview,uiscrollview,Ios,Swift,Uitableview,Uiscrollview,加载视图控制器时,我想将特定的表视图单元格滚动到顶部 这种方法很有效。但当单元格高度动态变化时,单元格不会滚动到顶部,而是滚动到中心 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "slideshowTableViewCe

加载视图控制器时,我想将特定的表视图单元格滚动到顶部

这种方法很有效。但当单元格高度动态变化时,单元格不会滚动到顶部,而是滚动到中心

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "slideshowTableViewCell", for: indexPath) as! slideshowTableViewCell

    if(self.scrollEnabled){

        let indexPath = NSIndexPath(row: self.imageId, section: 0)
        self.slideshowView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)


        self.scrollEnabled = false
    }

您应该调用
视图上的
scrollToRow
将出现

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tblList.scrollToRow(at: Your_desired_indexPath, at: .top, animated: true)
}

请停止调用cellForRowAt方法上的scrollToRow。这是非常糟糕的方法。您可以计算indexPath并在ViewWillDisplay上调用
scrollToRow
,使其只调用一次。