I honestly don't know how to subclass a UITableView. I'm super confused and looking for whatever help I can get. How do I go about "subclassing" a UITableView? Reason I need to do it is because I need for the table to respond to touches on the background so that keyboards can be hidden. I've tried googling but couldn't find anything. Any help is extremely appreciated!
Asked
Active
Viewed 6,643 times
2 Answers
10
Most of the time you shouldn't need to subclass UITableView
, so see if you can avoid doing so first. If you absolutely have to, create a subclass that inherits from UITableView
and override the touch-related methods in the implementation:
// 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

conmulligan
- 7,038
- 6
- 33
- 44
-
One thing to add: Don't forget to implement `UITableViewDelegate` and `UITableViewDataSource` if necessary – Raptor May 14 '12 at 02:17
-
1I think it's worth elaborating on why you shouldn't need to subclass UITableView. The reason is that you can accomplish a lot of customization by passing customized cells to a standard UITableView instance (via cellForRowAtIndexPath) . In other words, your controller acts as the data source for your table, and when the table is being set up, it will ask your controller for cells. This is what the standard tutorials are trying explain. – kris Jun 25 '14 at 03:57
-4
In your .h file declare it as a subclass:
@interface myViewController: UITableViewController
Then implement the UITableViewController delegate methods:
To detect touches on a cell write your logic in the tableView:didSelectRowAtIndexPath:
method: http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didSelectRowAtIndexPath:

Sheehan Alam
- 60,111
- 124
- 355
- 556