indexPathForCell returns nil since ios7

前端 未结 3 1194
陌清茗
陌清茗 2020-11-29 09:10

my app was running fine under ios6.1. tried the ios7 simulator and the following part does not work:

EditingCell *cell = (EditingCell*) [[textField superview         


        
相关标签:
3条回答
  • 2020-11-29 09:58

    Experiencing this problem in iOS 11, but not in 9 or 10, I overrode the func indexPath(for cell: UITableViewCell) -> IndexPath? method using the technique that @drexel-sharp detailed previously:

    override func indexPath(for cell: UITableViewCell) -> IndexPath? {
        var indexPath = super.indexPath(for: cell)
        if indexPath == nil { // TODO: iOS 11 Bug?
            let point = cell.convert(CGPoint.zero, to: self)
            indexPath = indexPathForRow(at: point)
        }
        return indexPath
    }
    
    0 讨论(0)
  • 2020-11-29 10:00

    I was finding cells the same way you were. Now I use this quick method if I have a button in a cell and know the tableview I'm in. It'll return the tableviewcell.

    -(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id)sender {
        CGPoint pos = [sender convertPoint:CGPointZero toView:tableView];
        NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pos];
        return [tableView cellForRowAtIndexPath:indexPath];
    }
    
    0 讨论(0)
  • 2020-11-29 10:07

    Your approach to find the "enclosing" table view cell of a text field is fragile, because is assumes a fixed view hierarchy (which seems to have changed between iOS 6 and iOS 7).

    One possible solution would be to traverse up in the view hierarchy until the table view cell is found:

    UIView *view = textField;
    while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
        view = [view superview];
    }
    EditingCell *cell = (EditingCell *)view;
    

    A completely different, but often used method is to "tag" the text field with the row number:

    cell.textField.tag = indexPath.row;   // in cellForRowAtIndexPath
    

    and then just use that tag in the text field delegate methods.

    0 讨论(0)
提交回复
热议问题