iOS - indexPathForRowAtPoint don't return correct indexPath with different cell height

前端 未结 2 1606
南笙
南笙 2021-01-22 02:38

I have UITableView that contains many cell. User can expand cell to see more content in this cell by push the expand button in this cell (only 1 cell can expand at time):

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-22 03:25

    One way to do it would be to add a UILongPressGestureRecognizer to each UITableViewCell (that all use the same selector), then when the selector is called you can get the cell via sender.view. Perhaps not the most memory efficient, but if the single gesture recognizer won't return the right row in certain situations, this way should work.

    Something like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        ...
    
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] 
      initWithTarget:self action:@selector(handleLongPress:)];
        [longPress setMinimumPressDuration:2.0];
        [cell addGestureRecognizer:longPress];
        [longPress release];
    
        return cell;
    }
    

    then

    - (void)handleLongPress:(UILongPressGestureRecognizer*)sender {  
        UITableViewCell *selectedCell = sender.view;
    }
    

提交回复
热议问题