Delay after didSelectRowAtIndexPath

I have a table view with custom cells, when I click on one of my cells, it shows me the next view manager, as it should be, but there is a delay that sometimes reaches 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. } 

}

+5
source share
3 answers

I have the same problem and it seems like this is an iOS bug. Solved using 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) } 
+19
source

Perhaps you will call a method

[tableView deselectRowAtIndexPath:indexPath animated:NO];

before a Push ViewController or other operation. how

 - (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 .... } 

this will solve my problem. when I custom viewController transition for Model Style

+6
source

You upload two images and create an instance of the view controller from the storyboard after someone clicks on the cell. This is a difficult process and may take some time depending on how complex your view controller is and how large these images are.

Now you can either move the image upload code to viewDidAppear() , or perhaps upload the images asynchronously, and then assign them.


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

 NSString *file = [[NSBundle mainBundle] pathForResource:@"image_name" ofType:@"jpg"]; UIImage *currentImage = [UIImage imageWithContentsOfFile:file]; 
0
source

Source: https://habr.com/ru/post/1212564/


All Articles