2016-09-09 42 views
1

在我的视图控制器中,我有一个tableView和加班我选择我的单元我的tableview移动有点上行我不知道为什么会发生,我试图找出为什么会发生,但我什么也没得到。下面是我的代码,如果任何机构可以找出最新的错误,那么它会对我有帮助。`tableView`向上移动,同时选择单元格

奥特莱斯和变量:

@IBOutlet weak var tableView: UITableView! 
@IBOutlet weak var headerView: UIView! 
@IBOutlet var verticalSpaceConstraint: NSLayoutConstraint! 
var lastVerticalOffset: CGFloat = 0 

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 

代表:

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 


    } 
} 

回答

0

您是删除的tableView刷新的代码DidSelect方法的一面。删除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 
} 
0

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

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

或在您的viewDidload尝试

self.automaticallyAdjustsScrollViewInsets = false 
+0

嗨,老兄,感谢响应,我使用'tableView.beginUpdates()'因为我改变了选择行和'self.automaticallyAdjustsScrollViewInsets的高度= FALSE '没有工作 –

相关问题