2010-08-10 40 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