UITextField subview of UITableViewCell, get indexPath of cell

前端 未结 5 1247
余生分开走
余生分开走 2020-11-30 08:30

I have added a UITextField as a subview of a UITableViewCell. Then I have added a target and selector so that I can know

相关标签:
5条回答
  • 2020-11-30 08:52

    Call [UIView superview] on the field to get the cell that it's in, then call [UITableView indexPathForCell:] on the cell to get the index path.

    UPDATE: on iOS 7 you need to call superview on that view too (extra layer of views); here's a category on UITableView that should work independent of iOS version:

    @interface UITableView (MyCoolExtension) 
    
    - (NSIndexPath *)indexPathForCellContainingView:(UIView *)view;
    
    @end
    
    @implementation UITableView (MyCoolExtension)
    
    - (NSIndexPath *)indexPathForCellContainingView:(UIView *)view {
        while (view != nil) {
            if ([view isKindOfClass:[UITableViewCell class]]) {
                return [self indexPathForCell:(UITableViewCell *)view];
            } else {
                view = [view superview];
            }
        }
    
        return nil;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-30 08:53

    I would like to share @Ertebolle code in swift -

    extension UITableView
    {
        func indexPathForCellContainingView(view1:UIView?)->NSIndexPath?
        {
            var view = view1;
            while view != nil {
                if (view?.isKindOfClass(UITableViewCell) == true)
                {
                    return self.indexPathForCell(view as! UITableViewCell)!
                }
                else
                {
                    view = view?.superview;
                }
            }
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:54

    Instead of recursively trying to find the superview, there's a simpler solution:

    Swift

    func indexPathForCellContainingView(view: UIView, inTableView tableView:UITableView) -> NSIndexPath? {
        let viewCenterRelativeToTableview = tableView.convertPoint(CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)), fromView:view)
        return tableView.indexPathForRowAtPoint(viewCenterRelativeToTableview)
    }
    

    Objective-C

    - (NSIndexPath *)indexPathForCellContainingView:(UIView *)view inTableView:(UITableView *)tableView {
        CGPoint viewCenterRelativeToTableview = [tableView convertPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)) fromView:view];
        NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:viewCenterRelativeToTableview];
        return cellIndexPath
    }
    

    I made this into a Gist for the Swift hearted

    0 讨论(0)
  • 2020-11-30 08:58

    Swift 4/5

    func indexPathForCellContainingView(view: UIView,
                                        inTableView tableView:UITableView) -> IndexPath? {
        let viewCenterRelativeToTableview =
            tableView.convert(CGPoint(x: view.bounds.midX, y: view.bounds.midY), to: view)
        return tableView.indexPathForRow(at: viewCenterRelativeToTableview)
    }
    
    0 讨论(0)
  • 2020-11-30 08:59

    Try this solution. It worked for me

       #pragma mark - Get textfield indexpath
    - (NSIndexPath *)TextFieldIndexpath:(UITextField *)textField
    {
        CGPoint origin = textField.frame.origin;
        CGPoint point = [textField.superview convertPoint:origin toView:self.TblView];
    
        NSIndexPath * indexPath = [self.TblView indexPathForRowAtPoint:point];
        NSLog(@"Indexpath = %@", indexPath);
        return indexPath;
    }
    
    0 讨论(0)
提交回复
热议问题