Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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 `tableView`在选择单元格时上移_Ios_Uitableview_Uiscrollview_Swift2 - Fatal编程技术网

Ios `tableView`在选择单元格时上移

Ios `tableView`在选择单元格时上移,ios,uitableview,uiscrollview,swift2,Ios,Uitableview,Uiscrollview,Swift2,在我的视图控制器中,我有一个tableView,当我选择我的单元格时,我的tableView向上移动了一点,我不知道它为什么会发生,我试图找出它为什么会发生,但我一无所获。下面是我的代码,如果任何人都能找出问题所在,那么它将对我非常有帮助 出口和变量: @IBOutlet weak var tableView: UITableView! @IBOutlet weak var headerView: UIView! @IBOutlet var verticalSpaceConstraint: NS

在我的视图控制器中,我有一个
tableView
,当我选择我的单元格时,我的
tableView
向上移动了一点,我不知道它为什么会发生,我试图找出它为什么会发生,但我一无所获。下面是我的代码,如果任何人都能找出问题所在,那么它将对我非常有帮助

出口和变量:

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var headerView: UIView!
@IBOutlet var verticalSpaceConstraint: NSLayoutConstraint!
var lastVerticalOffset: CGFloat = 0
extension myViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return reviewsArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TagDetailTableViewCell
        ..............................
        return cell
    }


    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        if  indexPath == selectedIndexPath {
            return UITableViewAutomaticDimension
        } else {
            return 100
        }

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        tableView.beginUpdates()

        if indexPath == selectedIndexPath {
            selectedIndexPath = NSIndexPath()
        }else {
            selectedIndexPath = indexPath
        }
        tableView.endUpdates()

        print( "tableView ORIIGN Y AFETER ", tableView.contentOffset.y)

        self.tableView.separatorStyle = .None
        self.tableView.separatorStyle = .SingleLine


    }
}
ViewDidLoad:

tableView.contentInset = UIEdgeInsets(top: headerViewOriginalHeight, left: 0, bottom: 0, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset
lastVerticalOffset = tableView.contentOffset.y


...........

footerView.addSubview(footerLabel)
footerView.addSubview(footerRetryButton)
footerView.addSubview(activityIndicatorView)

self.tableView.tableFooterView = footerView
代表:

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var headerView: UIView!
@IBOutlet var verticalSpaceConstraint: NSLayoutConstraint!
var lastVerticalOffset: CGFloat = 0
extension myViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return reviewsArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TagDetailTableViewCell
        ..............................
        return cell
    }


    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        if  indexPath == selectedIndexPath {
            return UITableViewAutomaticDimension
        } else {
            return 100
        }

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        tableView.beginUpdates()

        if indexPath == selectedIndexPath {
            selectedIndexPath = NSIndexPath()
        }else {
            selectedIndexPath = indexPath
        }
        tableView.endUpdates()

        print( "tableView ORIIGN Y AFETER ", tableView.contentOffset.y)

        self.tableView.separatorStyle = .None
        self.tableView.separatorStyle = .SingleLine


    }
}

您正在删除DidSelect方法中的tableView重载代码。删除
tableView.beginUpdates()
tableView.endUpdates()
,现在只需重新加载tableView的单个单元格

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

      if indexPath == selectedIndexPath {
        selectedIndexPath = NSIndexPath()
      }else {
        selectedIndexPath = indexPath
      }

     NSArray* rowsToReload = [NSArray arrayWithObjects:indexPath, nil];
     [myUITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

     self.tableView.separatorStyle = .None
     self.tableView.separatorStyle = .SingleLine
}

您正在删除DidSelect方法中的tableView重载代码。删除
tableView.beginUpdates()
tableView.endUpdates()
,现在只需重新加载tableView的单个单元格

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

      if indexPath == selectedIndexPath {
        selectedIndexPath = NSIndexPath()
      }else {
        selectedIndexPath = indexPath
      }

     NSArray* rowsToReload = [NSArray arrayWithObjects:indexPath, nil];
     [myUITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

     self.tableView.separatorStyle = .None
     self.tableView.separatorStyle = .SingleLine
}

didselectrow
方法中删除
tableView.beginUpdates()和
tableView.endUpdates()
。您不需要在这种情况下使用它

管理数组以跟踪所选行,并相应地管理
cellforrow
,并从
didselectrow
重新加载表

或者在
视图中加载
试试

    self.automaticallyAdjustsScrollViewInsets = false

didselectrow
方法中删除
tableView.beginUpdates()和
tableView.endUpdates()
。您不需要在这种情况下使用它

管理数组以跟踪所选行,并相应地管理
cellforrow
,并从
didselectrow
重新加载表

或者在
视图中加载
试试

    self.automaticallyAdjustsScrollViewInsets = false

嘿,伙计,感谢您的响应,我正在使用
tableView.beginUpdate()
,因为我正在更改所选行的高度,并且
self.automaticallyAdjustsScrollViewInsets=false
不起作用嘿,伙计,感谢您的响应,我正在使用
tableView.beginUpdate()
因为我正在更改所选行的高度,而self.automaticallyAdjustsScrollViewInsets=false
不起作用