0

I'm creating a UiTableView with 2 different cell nibs. it is working perfectly fine expect 1 specific scenario.

ISSUE: On cell selection, i perform some action in didSelectRowAtIndexPath. After didSelectRowAtIndexPath execution, iOS invokes cellForRowAtIndexPath method and recreates that clicked cell.

What is the affects of recreation: My cells heights are dynamic. When UiTableView recreates any cell from middle, it causes unnatural jerks on scrolling back/up.

Below is the code snippet of my methods. Any help would be appriciated :)

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


        var post = self.posts[indexPath.row]

        if(post.valueForKey(MEDIA) != nil) {
            var cell:PostImageTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("PicCell") as PostImageTableViewCell
            cell.loadPostData(post)
            return cell
        } else {
            var cell:PostTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as PostTableViewCell
            cell.loadPostData(post)
            return cell
        }
    }

Selection method:

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

        self.selectedRow = indexPath.row
        self.performSegueWithIdentifier("detailSegue", sender: nil);
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

Call Stack:

enter image description here

Ahmad Raza
  • 2,850
  • 1
  • 21
  • 37
  • If you comment out the `deselectRow` command, just as a test, does that solve the problem? – matt Jan 01 '15 at 17:57
  • @matt: Nope. i commented deselectRow and still cell is recreating. – Ahmad Raza Jan 01 '15 at 18:13
  • I was afraid of that. So what's causing `cellForRow` to be called? Merely selecting or deselecting a row should not do it. Put a breakpoint on `cellForRow` so you can inspect the call stack and find out. Maybe you are calling `reloadData()` in an observer on `selectedRow` or something. – matt Jan 01 '15 at 18:21
  • Maybe try removing (deleting) the cell each time it's selected, and inserting the new cell? Curious to see what that would do... – Anna Dickinson Jan 01 '15 at 18:24
  • @matt: Please check call stack screenshot(See Question). I'm not calling reloadData() from anywhere in code. – Ahmad Raza Jan 01 '15 at 18:31
  • @AnnaDickinson: What do you mean by deleting? I'm not saving references of cells in my code. Btw, It is already inserting again on selection and i just want to stop this recreation/reinsertion. – Ahmad Raza Jan 01 '15 at 18:33
  • What do you mean by "recreates that clicked cell"? What is your evidence that the cell is being recreated? – rdelmar Jan 01 '15 at 18:47
  • Insert/delete the row -- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instm/UITableView/insertRowsAtIndexPaths:withRowAnimation: – Anna Dickinson Jan 01 '15 at 19:14
  • Now, but it looks like you are doing something that triggers layout. Again, merely selecting the row would not do that. I'm sorry, you're just not giving enough info to analyze from here. There is clearly a lot more going on than you are willing to reveal. – matt Jan 01 '15 at 19:25
  • @matt: Thanks man, I'm using cell nibs that are actually using autolayout. But the TableLayout is in separate storyboard, it doesn't uses autolayout. Please let me know if you require any information, I'll mention that here. – Ahmad Raza Jan 01 '15 at 19:43
  • What is your segue doing? – Mike Taverne Jan 01 '15 at 21:49
  • @MikeTaverne: It is just a push segue, Takes to Cell-Detail-Page. – Ahmad Raza Jan 01 '15 at 23:51
  • So if tapping the cell takes you to the detail page, when does the problem with your table view show itself? – Mike Taverne Jan 02 '15 at 00:21
  • When i get back to uiTableView & scrolls up, It scrolls with jerks. I just noticed that the issue isn't because of didSelect, If i just switch between tabs and get back to UiTableViewController, It scrolls down a little bit and recreates last visible cell. – Ahmad Raza Jan 02 '15 at 00:25
  • @matt: I figured out the reason: self.tableView.rowHeight = UITableViewAutomaticDimension is causing problems. What do you suggest? I am using self sized cells in tableview. What should i use instead of UiTableViewAutomaticDimension? – Ahmad Raza Jan 02 '15 at 10:31
  • @matt same problem here: http://stackoverflow.com/q/26917728/2105241 – Ahmad Raza Jan 02 '15 at 12:06

0 Answers0