Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return NSIndexPath from custom cell ? (UITableView)

I need to get the NSIndexPath for a custom cell in a UITableView. Here's the problem: I send a NSNotification from my custom cell to my UITableViewController when editingDidBegin gets called from a UITextField in my custom cell. In my UITableViewController, I resize the UITableView when the UITextField began editing, and then want the table view to scroll to the Cell in which the UITextField is first responder. But I can't figure out how to return the indexPath of the cell where the UITextField is being edited. I have tried so many ways, but its still not working. One way is this: in my cstomCell class i select the row using [self setSelected:YES] and in my TV controller then if I NSLog the row of [self.tableV indexPathForSelectedRow] it always returns 0 even though its always not 0.

like image 914
JonasG Avatar asked Sep 08 '11 20:09

JonasG


People also ask

How do I get indexPath from cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

How do you add a space between UITableView cells?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.


1 Answers

Just give the cell a value for its tag property. Then you can get that cell by calling this

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];

then once you have the cell you can get the NSIndexPath like this

NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];

Since you have a UITextField in your custom cell you can place cell.textField.delegate = self; in the

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

data source method. That way you will not have to setup a NSNotification in the Custom Cell. Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row;

Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];        

}

The above UITextField delegate method should get you the indexPath for the cell you have currently selected.

like image 95
ms83 Avatar answered Oct 31 '22 03:10

ms83