簡體   English   中英

觸摸UITableview的事件

[英]Touch Events for UITableview

我有一個UIView覆蓋子類UITableview 問題是,我無法使tableview滾動。 我嘗試覆蓋touchesBegantouchesMovedtouchesEnded 然后,我嘗試覆蓋hittest,但這似乎沒有影響。

-(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];
}

如果您的UIViewUITableView之上,則所有觸摸事件將降落在該UIView中,並且您的UITableView將不會滾動。 您需要為最高級的UIView禁用交互

當您需要創建專門的UITableView ,最好總是使用包含UITableViewUIViewController ,而不是盡可能地隨意擺弄UITableView層次結構。 Apple在tableview層次結構中做了很多工作,這使得添加自己的自定義視圖常常很麻煩。 因此,簡短的答案是:避免將自己的視圖插入tableView層次結構。

實際上,我幾乎再也不會使用UITableViewController子類了。 我總是發現自己需要以一種UITableViewController不容易支持的方式自定義視圖控制器-例如在創建視圖時覆蓋表View的視圖。 而是按以下方式創建您的控制器:

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) IBOutlet UITableView *tableView

@end

如果使用Interface Builder,請將tableView放到視圖控制器的視圖中,並將委托和數據源設置為視圖的所有者。 或者,您可以通過viewDidLoad方法在代碼中執行相同的操作。 無論哪種情況,在這一點上,您都可以像對待UITableViewController一樣對待視圖控制器,其附加好處是能夠執行諸如將視圖插入self.view而不會發生任何麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM