0

I am not really sure why this runs slow. Sometimes I can tap the row and it opens the popup instantly. Sometimes I can tap the row and it takes 2-3 seconds to load. Almost seems like maybe the code is confused on getting the cell.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedItem = self.filteredTransactions[indexPath.row]
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? BudgetHomeCell {
        if let addTXView = self.storyboard?.instantiateViewControllerWithIdentifier("BHAddTXVC") as? BHAddTXVC {
            addTXView.modalPresentationStyle = .Popover
            addTXView.preferredContentSize = CGSizeMake(200, 200)
            let popover = addTXView.popoverPresentationController
            popover?.permittedArrowDirections = .Any
            popover?.delegate = self
            popover?.sourceView = cell.valueLabel
            popover?.sourceRect = cell.valueLabel.bounds
            addTXView.selectedTX = selectedItem
            self.presentViewController(addTXView, animated: true, completion: nil)
        }
    }
}

Any suggestions for better performance while presenting the popover from the value label within the table view cell? While tapping I have checked the debug navigator and there are no CPU or memory spikes. This happens both for a simulated iPad and an iPad Air 2. I did have a swipe gesture running which threw it off. I removed this and its still presenting the popup very slow in some cases.

matthew
  • 343
  • 2
  • 15
  • 1
    Here's a wild and crazy guess: wrap the whole interior of your `didSelectRowAtIndexPath` in a `delay` block (for my `delay` function, see http://stackoverflow.com/a/24318861/341994). It can be a `delay` of `0`, or `0.1`. See if that makes any difference. If it doesn't help, okay, we failed, take it back out again. – matt Apr 23 '16 at 02:54
  • as @matt suggested, you can try to explicitly make the code run on the main thread. – Cheng-Yu Hsu Apr 23 '16 at 05:17
  • Should have realized that, guess I was really wondering if there was something with the code that was causing it to process slowly that I was doing incorrectly. Using Matt's suggestion works. – matthew Apr 25 '16 at 03:26

2 Answers2

3

I Don't really know the reason why. But this solved my problem by putting DispatchQueue.main.async {} in didSelectRowAtItemAt.

Ly Boung
  • 156
  • 7
2

Someone answered this in another post. Check it out here: https://stackoverflow.com/a/27227446/4740794

The trick is to manually deselect the row after it is selected. It worked great for me. Make sure to use or store the selected row if needed before deselecting it.

Community
  • 1
  • 1
Coder1224
  • 1,429
  • 2
  • 15
  • 21