ßaron
ßaron

Reputation: 69

Delay after didSelectRowAtIndexPath

I have a tableview with custom cells, when I tap on one of my cells it shows me the next viewcontroller as it should be, but there is a delay which sometimes goes up to 5 seconds.

How can I get rid of the delay?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let person = persons[indexPath.row]

    var personViewController: PersonViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PersonViewController") as PersonViewController

    personViewController.name = person.name
    personViewController.imageName = person.image

    self.presentViewController(personViewController, animated: true, completion: nil)
}

PersonViewController

import UIKit

class PersonViewController: UIViewController {

@IBOutlet weak var personImage: UIImageView!
@IBOutlet weak var overlayImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!

var name: String?
var imageName: String?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.personImage.image = UIImage(named: imageName!)
    self.overlayImage.image = UIImage(named: "image_overlay.png") //this is a filter
    self.nameLabel.text = imageName
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Upvotes: 7

Views: 4733

Answers (3)

ZHZ
ZHZ

Reputation: 2098

I have the same problem and it seems that this is a bug of iOS. Solved with the following solution.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 
    Int64(0 * Double(NSEC_PER_SEC))), 
    dispatch_get_main_queue()) { () -> Void in
        self.presentViewController(personViewController, animated: true,completion: nil)
    }

Swift3 version:

DispatchQueue.main.async {
    self.presentViewController(personViewController, animated: true,completion: nil)
}

Upvotes: 27

Beyond Chao
Beyond Chao

Reputation: 115

Maybe you will call the method

[tableView deselectRowAtIndexPath:indexPath animated:NO];

before Push ViewController or Other Operation. Like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1. manual call this method to deSelect Other Cell
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    // 2. than do other operation
    PushViewController Or Some Animation ....
}

that`s solve my problem . when i custom transition viewController for Model Style

Upvotes: 6

Armin
Armin

Reputation: 1150

You are loading two images and instantiating a view controller from the storyboard after someone taps on a cell. This involves a heavy process, and can take time depending on how complicated your view controller is, and how big those images are.

Now you could either move your image loading code to viewDidAppear() or maybe load the images asynchronously and then assign them.


Another way could be to use imageWithContentsOfFile: to load the images. Apparently this method is faster and uses less memory.

NSString *file = [[NSBundle mainBundle] pathForResource:@"image_name" ofType:@"jpg"];

UIImage *currentImage = [UIImage imageWithContentsOfFile:file];

Upvotes: 0

Related Questions