3

(他のものに加えて)という名前のカスタムUItableViewCellクラスがあり、その. デリゲート メソッドでは、それがどのセクションに属しているかを検出しようとしています。このような:EditableTableViewCellUITextFieldcontentViewTextfield

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    //traverse up in the view hierarchy until we find the cell's content view.
    while (![contentView isKindOfClass:[EditableTableViewCell class]] || contentView.superview == nil) {
        contentView = [contentView superview];
    }
    EditableTableViewCell *thiscell = (EditableTableViewCell*)contentView;
    NSIndexPath *indexpath =[[self tableView]indexPathForRowAtPoint:thiscell.center];

これにより正しい結果が得られ 、ビュー階層をナビゲートする同様のソリューションもここで見られました。これが唯一の解決策なのか、それとも利用可能なすべてのセルのループを伴わない、より適切なものがあるのか​​ 疑問に思っています。

4

2 に答える 2

5

これを試してください。ビューをループする必要はありません

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    CGPoint center = [self.tableView convertPoint:textField.center fromView:contentView];
    NSIndexPath *indexpath =[[self tableView] indexPathForRowAtPoint:center];
    NSLog(@"indexPath:%@", indexpath);
}
于 2013-09-26T08:46:36.620 に答える
0

なぜだめですか

NSIndexPath *indexpath = [(UITableView*)self.superview indexPathForCell:self]

?

于 2013-09-26T08:50:08.700 に答える