NSIndexPath
のカスタム セルの を取得する必要がありUITableView
ます。ここに問題があります。カスタム セルの a から呼び出されたときNSNotification
に、カスタム セルから a を自分に送信します。私のでは、が編集を開始したときに のサイズを変更し、テーブル ビューをがファーストレスポンダであるセルにスクロールするようにします。しかし、編集中のセルのを返す方法がわかりません。私は非常に多くの方法を試しましたが、まだ機能していません。1 つの方法は次のとおりです。私の cstomCell クラスでは、TV コントローラーを使用して行を選択します。その場合、その行は常に 0 ではありませんが、常に 0 を返します。UITableViewController
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;
}
各セルに複数のテックスフィールドがある場合、これ(以下の前の回答)は機能しますか?:
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つのテックスフィールドがあり、テーブルビューは動的であるため、ユーザーはより多くのセルを挿入できます...しかし、ユーザーが入力したテキストを保存する方法に問題があります各テックスフィールド。「cell.textField.tag = indexPath.row」で、コントローラーが私がいるテックスフィールドを識別する可能性はありますか?
私は自分のセルに次のようにタグを付けるのが好きです:
/* 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;
}
もう 1 つの方法は、カスタム デリゲートを使用して beginEditing データを渡し、セル モデルを含めることです。これを使用して、データの保存方法に応じてパスを把握できます。
私の github プロジェクトを確認してください。このケースを処理するコード、キーボードのサイズ変更、次の編集可能なセルへのタブ移動が含まれています: https://github.com/andrewzimmer906/XCell