从UIButton发件人返回错误的tableViewCell

时间:2015-04-14 14:57:38

标签: ios ipad

对于ios 7及更高版本,返回的单元格始终是第一个单元格,即indexpath.row = 0。虽然我正在考虑带有额外.superview的单元格内部的scrollview。

- (UITableViewCell *)getCellforButton:(UIButton *)sender
{
    UITableViewCell *cell;

    //Check for iOS version and get the cell accordingly
    NSArray *versionComponenets = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

    if ([[versionComponenets objectAtIndex:0] intValue] >= 7)
    {
        // iOS-7 code[current] or greater
        //Additional superview call to account for new scrollview inside of cells
        cell = (UITableViewCell *)sender.superview.superview.superview;
    }
    else
    {
        // iOS-6 code
        cell = (UITableViewCell *)sender.superview.superview;
    }

    return cell;
}

1 个答案:

答案 0 :(得分:2)

使用UIView's convertPoint:toView method获取UITableViewCell

-(UITableViewCell *)getCellforButton:(UIButton *)sender
{
    CGPoint btnPosition = [sender convertPoint:CGPointZero toView:yourTableViewHere];
    NSLog(@"btnPositionn : %@",NSStringFromCGPoint(btnPosition));
    NSIndexPath *curIndexPath = [yourTableViewHere indexPathForRowAtPoint:btnPosition];
    id cell = [yourTableViewHere cellForRowAtIndexPath:curIndexPath];
    if (cell)
    {
        UITableViewCell *curCell = cell;
        return curCell;
    }
    else
        return nil
 }