Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDK: How to get a cell in a table view that is not visible?

How do I get the cell for an indexPath which is not currently visible in the table? (cell is out of range)

Code for getting my cell:

NSString *name = [[(ELCTextfieldCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] rightTextField] text];

-cellForRowAtIndexPath... returns nil because the cell at the required indexPath is out of range. So how do I get the correct cell and not nil?

like image 869
Daniel Mühlbachler Avatar asked Jun 26 '11 12:06

Daniel Mühlbachler


People also ask

How do I register a custom table view cell?

Registering the cell To do that we register the XIB file in the view controller that has the UITableView . Then, open HabitsTableViewController. swift to finish registering the cell. Add the following line of code inside the viewDidLoad() method.

How do I use table view in Xcode?

Add a table view to your interface. To add a table view to your interface, drag a table view controller ( UITableViewController ) object to your storyboard. Xcode creates a new scene that includes both the view controller and a table view, ready for you to configure and use.


2 Answers

This is not how it works. You need to grab and store the information as soon as it is entered or changed. It may easily get out of scope and you cannot guarantee your cell lives long enough. Well, technically you can hold onto it (and always return the very same cell for the same index path), but I'd question that design.

like image 187
Eiko Avatar answered Nov 07 '22 06:11

Eiko


In continue to @Raphael's answer, Here is the (working) swift 3 solution:

UITableViewCell cell = self.tableView(self.tableView, cellForRowAt: indexPath)

like image 36
Yonathan Goriachnick Avatar answered Nov 07 '22 07:11

Yonathan Goriachnick