我需要NSIndexPath
在UITableView
. 问题出在:我从自定义单元格中将 a 发送NSNotification
到我的自定义单元格中的 a 被调用UITableViewController
时。在我的中,我调整了开始编辑的大小,然后希望表格视图滚动到第一响应者所在的单元格。但我不知道如何返回正在编辑的单元格的。我已经尝试了很多方法,但它仍然无法正常工作。一种方法是:在我的 cstomCell 类中,我使用和在我的电视控制器中选择行,然后如果我它的行总是返回 0,即使它总是不为 0。editingDidBegin
UITextField
UITableViewController
UITableView
UITextField
UITextField
indexPath
UITextField
[self setSelected:YES]
NSLog
[self.tableV indexPathForSelectedRow]
4 回答
只需给单元格的tag
属性值。然后你可以通过调用这个来获取那个单元格
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
然后一旦你有了单元格,你就可以像这样获得 NSIndexPath
NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];
由于您的自定义单元格中有一个 UITextField ,因此您可以cell.textField.delegate = self;
将
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
数据源方法。这样您就不必在自定义单元格中设置 NSNotification。同样在同样的方法中,您可以像这样标记单元格文本字段和这样cell.textField.tag = indexPath.row;
的单元格cell.tag = indexPath.row;
现在您已经设置了 UITextField 委托,您现在可以将此方法放在您的 UITableViewController 类中
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
上面的 UITextField 委托方法应该为您提供当前选择的单元格的 indexPath。
试试这个代码..
- (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;
}
如果您在每个单元格中有多个 texfields,这(您在下面给出的答案)是否仍然有效?:
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];
由于您UITextField
在自定义单元格中有一个,因此您可以将其放置cell.textField.delegate = self;
在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
数据源方法。同样在同样的方法中,您可以像这样标记两个单元格文本字段 cell.textField.tag = indexPath.row; 像这样的单元格 cell.tag = indexPath.row;
现在您已经设置了 UITextField 委托,您现在可以将此方法放在您的 UITableViewController 类中
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
我有一个包含 8 个单元格的表格视图,在每个单元格中我有 6 个 texfields,而且表格视图是动态的,因此用户可以插入更多单元格......但是我在如何保存用户输入的文本时遇到了麻烦每个texfield。是否有可能使用“cell.textField.tag = indexPath.row”,控制器识别我所在的 texfield?
我喜欢这样标记我的单元格:
/* 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;
}
另一种方法是使用自定义委托传递 beginEditing 数据,并包含一个单元模型,然后您可以根据存储数据的方式使用它来确定路径。
查看我的 github 项目,它有处理这种情况的代码,以及调整键盘大小和切换到下一个可编辑单元格:https ://github.com/andrewzimmer906/XCell