更改表格宽度时,Swift Tableview向上移动

时间:2017-11-06 17:00:51

标签: ios swift uitableview uiscrollview

我的视图看起来像this,我在滚动视图旁边有一个tableview。

tableview具有固定的tablecell高度,没有截面和垂直滚动。 (只是标准的表格视图)

scrollview可以水平滚动,这样可以使tableview宽度减小或增加。 (而scrollview则相反)

到目前为止一切正常。

问题是当滚动滚动视图(并因此增加或减少tableview宽度)时,tableview只是稍微向上滚动。

我的第一个想法是tableviewoffset.y会改变,但显然offset.y根本没有改变。

有谁知道如何解决这个问题?

编辑: 我明白我的问题还不是很清楚。 我构建了一个示例项目来说明它。 (Download link

按照以下步骤操作。

  1. 运行项目
  2. 将桌面视图(左侧)一直向下滚动(它的编号为49)
  3. 向左或向右拖动“测试”
  4. 回顾一下tableview,你可以注意到tableview“滚动”了。
  5. (步骤2b滚动到任何表格单元格编号(1除外),也会出现问题;编号49只是示例)

    编辑2: 问题已经解决了......

    插入以下行解决了问题。

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
    {
            return 55
    }
    

    非常感谢!

    (我的viewcontroller代码)

    class ViewController: UIViewController
    {
        @IBOutlet weak var TableView: UITableView!
        @IBOutlet weak var ScrollView: UIScrollView!
    
        @IBOutlet weak var scrollViewWidth: NSLayoutConstraint!
        @IBOutlet weak var TableViewWidth: NSLayoutConstraint!
    
        var defaultTableViewWidth:CGFloat!
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            self.defaultTableViewWidth = self.TableViewWidth.constant
            self.scrollViewWidth.constant = 350
        }
    }
    
    extension ViewController : UITableViewDelegate, UITableViewDataSource
    {
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 55
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            return 50
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "TabCell", for: indexPath) as! TabCell
            cell.clientLabel.text = "E"
            cell.debtNumberLabel.text = "\(indexPath.row)"
            return cell
        }
    
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
            print("b - offset: \(self.TableView.contentOffset.y)")
            print("b - rowheight: \(self.TableView.rowHeight)")
            print("b - cont height: \(self.TableView.contentSize.height)")
    
            if scrollView == ScrollView
            {
                if scrollView.contentOffset.x < 0
                {
                    self.TableViewWidth.constant += abs(scrollView.contentOffset.x)
                }
                else if scrollView.contentOffset.x > 0 && self.TableViewWidth.constant >= 60
                {
                    self.TableViewWidth.constant = max(self.TableViewWidth.constant - scrollView.contentOffset.x, 0.0)
    
                    if self.TableViewWidth.constant >= 60
                    {
                        scrollView.contentOffset.x = 0
                    }
                    else
                    {
                        self.TableViewWidth.constant = 60
                    }
                }
            }
    
            print("a - offset: \(self.TableView.contentOffset.y)")
            print("a - rowheight: \(self.TableView.rowHeight)")
            print("a - cont height: \(self.TableView.contentSize.height)")
            print(" ")
    
        }
    
        func animateHeader(height: CGFloat)
        {
            self.TableViewWidth.constant = height
            UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: UIViewAnimationOptions(), animations: {
                self.view.layoutIfNeeded()
            }, completion: nil)
        }
    
        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
        {
            if self.TableViewWidth.constant > self.defaultTableViewWidth
            {
                animateHeader(height: self.defaultTableViewWidth)
            }
        }
    
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
        {
            if self.TableViewWidth.constant > self.defaultTableViewWidth
            {
                animateHeader(height: self.defaultTableViewWidth)
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

以下信息有助于调试

  • 您使用什么限制?
  • 桌面框架位置是固定的还是在拖动时会改变?也许有关正在发生的事情的视频有助于调试此事。

(我暂不发表评论,这就是为什么我将此作为答案发布)