0

I get the error stated in the topic for unknown reason as the cell I reference(in DownloadProfilePicture) is visible. Basically, I am trying to replicate/adapt what is done in LazyTableViewImages

override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell") self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell") tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell") tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell") self.tableView.delegate = self self.tableView.dataSource = self } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let nodeCount = arrayOfPosts.count; let cell = UITableViewCell() tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell if (nodeCount > 0) { if (indexPath.row % 2 == 0){ // Set up the cell representing the app var cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell let post = arrayOfPosts[indexPath.row] cell.userName.text = post.userName cell.timeSincePosted.text = post.timeSincePosted // Only load cached images; defer new downloads until scrolling ends if (post.profileImage == nil) { if (!tableView.dragging && !tableView.decelerating) { downloadProfileImage(post, indexPath: indexPath) return cell } // if a download is deferred or in progress, return a placeholder image cell.profileImage.image = UIImage(named: "titanic.jpg") } else { cell.profileImage.image = post.profileImage } return cell } func downloadProfileImage(post: Post, indexPath: NSIndexPath){ println(self.tableView.indexPathsForVisibleRows()) println(indexPath) let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath) as! topCell cell.profileImage.image = UIImage(named: "img1") } 

visiblePaths:

 ( "<NSIndexPath: 0x7888f7b0> {length = 2, path = 0 - 0}", "<NSIndexPath: 0x788a4d60> {length = 2, path = 0 - 1}" ) 

cell:

<NSIndexPath: 0x788a3c80> {length = 2, path = 0 - 1} 
5
  • 1
    let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell Commented Jul 5, 2015 at 15:13
  • I have tried your suggestion. Unfortunatly, it did not change anything. Commented Jul 5, 2015 at 15:23
  • 1
    FWIW The Cocoa* convention is that variables and method names start wit a lowercase letter, class names start with an upper case letter. This when I see: topCell I initially think that is is a variable. Conventions do help. Commented Jul 5, 2015 at 15:30
  • It would seem that there is no cell at the indexPath and you are explicitly unwrapping it. If the cell is not visible it will probably not exist. Never explicitly unwrap anything that could ever, under any circumstances be nil. From the docs: An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range. It is visible? Commented Jul 5, 2015 at 15:34
  • As you may see from the last part of my question ( the print out of values of visiblePaths and the cell I am referencing) , it is visible. Commented Jul 5, 2015 at 15:41

1 Answer 1

2

in the method the table view cell is created twice. Try a structure like this, it must be guaranteed that a table view cell is returned

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let nodeCount = arrayOfPosts.count; let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell if (nodeCount > 0) { if (indexPath.row % 2 == 0){ let post = arrayOfPosts[indexPath.row] cell.userName.text = post.userName cell.timeSincePosted.text = post.timeSincePosted // Only load cached images; defer new downloads until scrolling ends if (post.profileImage == nil) { if (!tableView.dragging && !tableView.decelerating) { downloadProfileImage(post, indexPath: indexPath) return cell } // if a download is deferred or in progress, return a placeholder image cell.profileImage.image = UIImage(named: "titanic.jpg") } else { cell.profileImage.image = post.profileImage } } } return cell } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I just did not include the full method, because it is a little bit long. You can check it out here pastebin.com/q7rLbLq9
make sure that the cell is instantiated only once in each branch. There are still two places. Remove the first (global) one. And avoid calling cellForRowAtIndexPath in other methods explicitly. Update the model and reload the table view
Alright! But then why apple calls cellForRowAtIndexPath in other methods? from "LazyTableViewImages": pastebin.com/pV1C0Uvg

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.