简体   繁体   中英

Touch Events for UITableview

I have a UIView overlaying a subclassed UITableview . The problem is that ,I cant get the tableview to scroll. I have tried overriding touchesBegan , touchesMoved , touchesEnded . I then tried to override hittest but that seemed to have no affect.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    NSLog(@"SMTable.touches began %@",NSStringFromCGPoint(touchPoint));
    [super touchesBegan:touches withEvent:event];

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    NSLog(@"SMTable.touches moved %@ for :%p",NSStringFromCGPoint(touchPoint),touch.view);
    [super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    NSLog(@"SMTable.touches ended %@",NSStringFromCGPoint(touchPoint));
    [super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
    [super touchesCancelled:touches withEvent:event];
}
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    //NSLog(@"SMTable.hitTest %@",NSStringFromCGPoint(point));
    return [super hitTest:point withEvent:event];
}

If your UIView is above your UITableView , then all touch events will land in that UIView and your UITableView will not scroll. You need to disable interaction for your top most `UIView˜

When you need to create a specialized UITableView you are almost always better off using a UIViewController that contains a UITableView rather than mucking around in the UITableView hierarchy if at all possible. Apple are doing quite a bit of stuff in the tableview hierarchy which makes adding your own custom views to it often go awry. So, the short answer is : avoid inserting your own views into the tableView hierarchy.

In fact, I almost never use a UITableViewController subclass anymore. I always find myself needing to customize the view controller in a way that isn't easily supported from a UITableViewController -- such as creating a view to overlay the tableView as you are doing. Instead, create your controller like this:

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) IBOutlet UITableView *tableView

@end

If you are using Interface Builder, drop your tableView into the view controller's view and set the delegate and datasource to the view's owner. Or you can do the same thing in code via the viewDidLoad method. In either case, at this point you can treat the view controller exactly as if it were a UITableViewController with the added benefit of being able to do things like inserting views into self.view without things going horribly awry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM