2010-08-10 88 views
3

我真的不知道如何继承UITableView。我非常困惑,并寻找我可以得到的任何帮助。我如何去“继承”一个UITableView?我需要这样做的原因是因为我需要表格来响应背景的触摸,以便可以隐藏键盘。我试过Google搜索,但找不到任何东西。任何帮助非常感谢!如何继承UITableView?

回答

10

大多数时候你不需要子类UITableView,所以看看你是否可以避免这么做。如果您绝对有必要,创建一个从UITableView继承的子类,并覆盖在实现触摸相关的方法:

// MyTableView.h 

@interface MyTableView : UITableView { 
} 

@end 

// MyTableView.m 

@implementation MyTableView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { 
    [super touchesCancelled:touches withEvent:event]; 
} 

@end 
+0

有一点补充:不要忘了如果需要的话实现'UITableViewDelegate'和'UITableViewDataSource' – Raptor 2012-05-14 02:17:02

+1

我认为值得详细说明为什么你不需要子类UITableView。原因是您可以通过将自定义单元格传递给标准UITableView实例(通过cellForRowAtIndexPath)来完成很多定制。换句话说,您的控制器充当您的表的数据源,并且在设置表时,它会要求您的控制器查找单元。这是标准教程试图解释的内容。 – kris 2014-06-25 03:57:44

-4
+3

的问题是有关的UITableView没有的UITableViewController – thrusty 2011-06-21 23:35:30