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
不起作用