UITableViewCell indexPath crash in iOS7

后端 未结 6 2030
不知归路
不知归路 2020-12-06 07:38

I\'ve a UITableViewCell in a UITableViewController. In my cell there is a button that when you click takes you to another view with a

相关标签:
6条回答
  • 2020-12-06 08:11

    In iOS7 UITableViewWrapperView is the superview of UITableViewCell, which means another superview for you:

    iOS7:

    UITableView *tableView = (UITableView *)cell.superview.superview;
    

    iOS6"

    UITableView *tableView = (UITableView *)cell.superview;
    

    Another change you can find it here:

    seems your code: UITableView table = (UITableView)[[[sender superview] superview] superview]; returns a UITableViewCell not a UITableView.

    Try the below:

    UITableView *table = (UITableView*)[[[[[sender superview] superview] superview] superview] superview];
    
    0 讨论(0)
  • 2020-12-06 08:13

    I had similar problem where I had to find the textFileds value from TableView

    if ([Util isiOSVerGreaterThen7]) { // iOS 7
                UIView *contentView = (UIView *)[textField superview];
                UITableViewCell *cell = (UITableViewCell *)[contentView superview];
                UITableView *tableView = (UITableView *)cell.superview.superview;
                NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    
                NSInteger sectionOfTheCell = [indexPath section];
                NSInteger rowOfTheCell = [indexPath row];
            }
            else { // iOS 6
                UITableViewCell *cell = (UITableViewCell *)[textField superview];
                UITableView *table = (UITableView *)[cell superview];
                NSIndexPath *pathOfTheCell = [table indexPathForCell:cell];
                NSInteger sectionOfTheCell = [pathOfTheCell section];
                NSInteger rowOfTheCell = [pathOfTheCell row];
            }
    

    And Util is

    + (BOOL)isiOSVerGreaterThen7 {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
            return NO;
        } else {
            return YES;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 08:19

    This is the best way I think:

    CGPoint subviewPosition = [sender convertPoint:CGPointZero toView:self.myTable];

    NSIndexPath* indexPath = [self.myTable indexPathForRowAtPoint:subviewPosition];

    You can simply get the index path here. Be it iOS7 or iOS6, it will return the correct index path

    0 讨论(0)
  • 2020-12-06 08:29

    Apple change the UITableViewCell hierarchy in iOS 7

    Using iOS 6.1 SDK

    <UITableViewCell>
       | <UITableViewCellContentView>
       |    | <UILabel>
    

    Using iOS 7 SDK

    <UITableViewCell>
       | <UITableViewCellScrollView>
       |    | <UITableViewCellContentView>
       |    |    | <UILabel>
    

    So if you want to get indexpath at button index then use following code Using iOS 6 or later SDK

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
        NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
        NSLog(@"index=%@",clickedButtonPath);
    

    Using iOS 7 or 7+ SDK

    UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
        NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
        NSLog(@"index=%ld",(long)clickedButtonPath.item);
    
    0 讨论(0)
  • 2020-12-06 08:29

    This is better to use

    NSIndexPath *indexPath = [self.callTable indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
        NSLog(@"%d",indexPath.row);
    
    0 讨论(0)
  • 2020-12-06 08:37

    It works for sure, but how super can we get?

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