UITableView contentOffset jumps after call to setContentOffset "programatically"

Hi guys,


I'll tell you my issue. After call to "setContentOffset" in my UITableView


self.tableView!.setContentOffset(CGPointMake(0, -227.5), animated: false)


when I begin a drag gesture over my UITableView the contentOffset is the same (0, -227.5) in delegate method "scrollViewWillEndDragging" but after in method "scrollViewDidScroll" the first contentOffset is very different, approximately (0, -152.5).


It shows a weird effect. The UITableView jumps sharply from point (0, -227.5) to (0, -152.5).


I attach a simple example code with the above problem:


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
  
    override func viewDidLoad() {
        super.viewDidLoad()
        /
      
        self.tableView!.contentInset = UIEdgeInsetsMake(82.5, 0, 0, 0)
    }
  
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
      
        self.tableView!.setContentOffset(CGPointMake(0, -227.5), animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
  
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
  
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
  
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
      
        return cell
    }
}


Any help?


Regards.

Replies

You aren't returning any rows resulting in the contentSize to be 0.

You can't scroll if there isn't any content to scroll.

Hi,


Thank you for your response, but you are wrong. In a UITableView with plain style you can scroll it. If I return for example 1 row in method "numberOfRowsInSection" the result is the same. Thank you anyway.


Regards.

HyperNova's answer was in regard to the code you posted which returns 0 rows, not in some hypothetical number you throw out later on.


Telling people they are wrong when you are asking for help will not get you very far.

Hi donarb,


In any case I want to offend anyone. Excuse me.


I think you misunderstood me. In my last post I said that even returning a number different by 0 the code does not work. You can try it if you want.


Regards.

actually, you don't have any cells in your tableview so you can scroll but you can't set the content offset and expect it to stay because there's no content there. so, you misundertood someone's advice a bit, but the trouble is the same. no cells = no content = no content size.