简体   繁体   English

从自定义单元格返回NSIndexPath? (UITableView的)

[英]Return NSIndexPath from custom cell ? (UITableView)

I need to get the NSIndexPath for a custom cell in a UITableView . 我需要在UITableView获取自定义单元格的NSIndexPath 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. 这是问题所在:当我从自定义单元格中的UITextField调用editingDidBegin时,我从自定义单元格向我的UITableViewController发送NSNotification 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. 在我的UITableViewController ,我在UITextField开始编辑时调整UITableView大小,然后希望表视图滚动到UITextField是第一响应者的Cell。 But I can't figure out how to return the indexPath of the cell where the UITextField is being edited. 但我无法弄清楚如何返回正在编辑UITextField的单元格的indexPath 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. 一种方法是:在我的cstomCell类中,我使用[self setSelected:YES]选择行,然后在我的电视控制器中,如果我NSLog [self.tableV indexPathForSelectedRow]的行,它总是返回0,即使它总是不为0。

Just give the cell a value for its tag property. 只需为单元格的tag属性赋值。 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

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

Since you have a UITextField in your custom cell you can place cell.textField.delegate = self; 由于您的自定义单元格中有UITextField,因此可以放置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. 这样您就不必在自定义单元格中设置NSNotification。 Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; 同样在这个方法中你可以标记你的单元格文本字段,就像这个cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row; 像这样的cell.tag = indexPath.row;

Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class 现在您已经设置了UITextField委托,现在可以将此方法放在UITableViewController类中

- (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. 上面的UITextField委托方法应该为您当前选择的单元格提供indexPath。

Does this (your answer that you gave before below)still works if you have multiple texfields in each cell?: 如果每个单元格中有多个texfield,这个(你在下面给出的答案)是否仍然有用?:

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; 由于您的自定义单元格中有UITextField ,因此可以放置cell.textField.delegate = self; in the 在里面

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

data source method. 数据源方法。 Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; 同样在这个方法中你可以标记你的单元格文本字段,就像这个cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row; 像这样的cell.tag = indexPath.row;

Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class 现在您已经设置了UITextField委托,现在可以将此方法放在UITableViewController类中

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

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

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

I have a tableview with 8 cells, and in each cell i have 6 texfields, and also the tableview is dynamic, so the user can insert more cells...but i´m having trouble in how to save the text the user enters in each texfield. 我有一个包含8个单元格的tableview,并且在每个单元格中我有6个texfields,并且tableview是动态的,因此用户可以插入更多单元格......但是我在如何保存用户输入的文本方面遇到了麻烦每个texfield。 Is it possible that with "cell.textField.tag = indexPath.row", the controller identify´s the texfield i´m in? 是否有可能使用“cell.textField.tag = indexPath.row”,控制器识别我所在的texfield?

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;
}

I like to tag my cells like so: 我喜欢这样标记我的细胞:

/* 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;
}

The other way is to pass the beginEditing data using a custom delegate, and include a cell model, which you can then use to figure out your path depending on how you are storing data. 另一种方法是使用自定义委托传递beginEditing数据,并包含一个单元模型,然后您可以根据存储数据的方式来确定路径。

Check out my github project, it has the code to handle this case, as well as resizing for the keyboard and tabbing to the next editable cell: https://github.com/andrewzimmer906/XCell 看看我的github项目,它有代码来处理这种情况,以及调整键盘和tabbing到下一个可编辑单元格的大小: https//github.com/andrewzimmer906/XCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM