TableView didSelectRowAtIndexPath未被调用

时间:2011-02-20 21:10:01

标签: iphone cocoa-touch xcode uitableview

在UITableView上,我使用自定义单元格代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.contentView];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.contentView];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


    if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&!swiped){
        //do something 
        NSLog(@"Tapped %d",indexRow);
        //return YES;
    } else if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&swiped) {
        swiped = FALSE;
    }

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.contentView];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


    if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
        //do something 
        NSLog(@"Swiped %d",indexRow);
        gestureStartPoint = [touch locationInView:self.contentView];
        swiped = TRUE;
    }
    else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
        //do something
    }
}

在我添加之前,didSelectRowAtIndexPath被调用,但是,现在它不是。我可以评论该部分,它工作正常。有人知道解决这个问题的方法吗?

编辑:发布工作代码

Customcell.h

#define kMinimumGestureLength  30
#define kMaximumVariance   5
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface CustomCell : UITableViewCell {
    UILabel *primaryLabel;
    UILabel *secondaryLabel;
    UIImageView *myImageView;


    CGPoint gestureStartPoint;
    int indexRow;
    BOOL swiped;
}
@property(nonatomic,retain)UILabel *primaryLabel;
@property(nonatomic,retain)UILabel *secondaryLabel;
@property(nonatomic,retain)UIImageView *myImageView;

@end

CustomCell.m

#import "CustomCell.h"


@implementation CustomCell
@synthesize primaryLabel,secondaryLabel,myImageView;

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;
    frame= CGRectMake(boundsX+10 ,0, 50, 50);
    myImageView.frame = frame;

    frame= CGRectMake(boundsX+70 ,5, 200, 25);
    primaryLabel.frame = frame;

    frame= CGRectMake(boundsX+70 ,30, 100, 15);
    secondaryLabel.frame = frame;
}

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier indexd:(int)indexd {
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
        // Initialization code
        primaryLabel = [[UILabel alloc]init];
        primaryLabel.textAlignment = UITextAlignmentLeft;
        primaryLabel.font = [UIFont systemFontOfSize:14];
        secondaryLabel = [[UILabel alloc]init];
        secondaryLabel.textAlignment = UITextAlignmentLeft;
        secondaryLabel.font = [UIFont systemFontOfSize:8];
        myImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:primaryLabel];
        [self.contentView addSubview:secondaryLabel];
        [self.contentView addSubview:myImageView];
        indexRow = indexd;
    }
    swiped = false;
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.contentView];
   // [super touchesbegan];
        [super touchesBegan:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.contentView];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


    if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&!swiped){
        //do something 
        NSLog(@"Tapped %d",indexRow);
        [super touchesEnded:touches withEvent:event];
        //return YES;
    } else if(deltaX < kMinimumGestureLength && deltaY < kMaximumVariance&&swiped) {
        swiped = FALSE;
    }
    //[super touchesEnded];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.contentView];    

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


    if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
        //do something 
        NSLog(@"Swiped %d",indexRow);
        gestureStartPoint = [touch locationInView:self.contentView];
        swiped = TRUE;
        //[self.superview.dataSource editCell];
    }
    else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
        //do something
    }
    //[super touchesMoved];
}

@end

2 个答案:

答案 0 :(得分:3)

我不是100%肯定,但我认为您需要调用超级方法才能继续保持默认行为。

[super touchesBegan:...];

[super touchesEnded:...];

[super touchesMoved:...];

每个人都有自己的方法。

答案 1 :(得分:0)

这似乎与this question相似。尝试使用[super touchesbegan] et al。

调用父方法