Return NSIndexPath from user cell? (UITableView)

I need to get NSIndexPath for a custom cell in a UITableView . Here's the problem: I send an NSNotification from my user cell to my UITableViewController when editingDidBegin receives a call from a UITextField in my user cell. In my UITableViewController I resize the UITableView when editing the UITextField , and then requires the table view to go to the cell in which the UITextField is the first responder. But I can't figure out how to return the indexPath cell where the UITextField edited. I tried so many ways, but it still does not work. One way: in my cstomCell class, I select the row using [self setSelected:YES] and in my TV controller, then if I NSLog string [self.tableV indexPathForSelectedRow] , it always returns 0, even if it is always not 0 .

+6
source share
4 answers

Just set the cell to the tag property. Then you can get this cell by calling it

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

then after you have a cell, you can get NSIndexPath, like this

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

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

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

data source method. This way you do not have to configure NSNotification in a custom cell. Also in the same method you can mark both text fields of your cells as follows: cell.textField.tag = indexPath.row; and a cell like this cell.tag = indexPath.row;

Now that you have set the UITextField delegate, now you can put 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 an indexPath for the cell you selected.

+14
source

try this code ..

 - (void)textFieldDidBeginEditing:(UITextField *)textField { UITableView *tableView; UITableViewCell* superViewCell = [self returnSuperCellViewOfTextField:textField]; if(superViewCell)[tableView scrollToRowAtIndexPath:[tableView indexPathForCell:superViewCell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } -(id)returnSuperCellViewOfTextField:(id)viewToCheck { id superView = [viewToCheck superview]; if([viewToCheck isKindOfClass:[UITableViewCell class]] && superView)return superView; else if(([viewToCheck respondsToSelector:@selector(superview)] && superView)) return [self returnSuperCellViewOfTextField:superView]; return nil; } 
0
source

Does this (your answer you gave below) still work if you have multiple tex fields in each cell ?:

 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 put cell.textField.delegate = self; in

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

data source method. Also in the same method you can mark both text fields of your cells, for example cell.textField.tag = indexPath.row; and a cell like this .tag = indexPath.row;

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

 - (void)textFieldDidBeginEditing:(UITextField *)textField { CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag]; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

I have a table view with 8 cells, and I have 6 tex fields in each cell, and also the tableview is dynamic, so the user can insert more cells ... but I have problems with how to save the text the user enters in each field tex. Is it possible that with "cell.textField.tag = indexPath.row" the controller identifies texfield i'm in?

0
source

I like to mark my cells as follows:

 /* Create a tag based on the section and row of a cell(used to retrive the cell for the beginEditingNextCell method. */ -(NSInteger)tagForIndexPath:(NSIndexPath*)path { return path.section * 1000 + path.row + 1; } 

Another way is to pass beginEditing data using a user delegate and include a cell model, which you can then use to determine your path depending on how you store the data.

Check out my github project, it has code to handle this case, as well as resizing for the keyboard and moving to the next editable cell: https://github.com/andrewzimmer906/XCell

-1
source

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


All Articles