Reputation: 49097
Is it normal, even possible to make classes that adopts the required and optional method such as in UITableViewDataSource. Then assign an object of this class as delegate in the UITableView?
Or is inheritance to be preffered?
Upvotes: 0
Views: 155
Reputation: 3054
Not at all: See the Apple's Objective-C guide's protocol section.
@interface MyTableViewDelegate : NSObject <UITableViewDelegate> {
// Declare instance variables here
}
// Declare other methods here, the delegate methods are optional, but not necessary
@end
The class above conforms to the UITableViewDelegate
. To let the compiler know it does, you have to put it between a <
and a >
(I don't know how they're called). To let the compiler know it conforms to multiple protocols, separate them by commas:
@interface MyTableViewDelegate : NSObject <UITableViewDelegate, UITableViewDataSource> {
// Declare instance variables here
}
// Declare other methods here, the protocol methods
// can be declared but it's not necessary
@end
You can also cast an object to let it conform to a protocol. Though this is not recommended. If an object does not implement a required function of the protocol, an exception probably will be thrown.
Casting:
[aTableView setDelegate:(id <UITableViewDelegate)myNotDeclaredAsDelegateThoughIsTableViewDelegateObject];
Hope it helps, ief2
Upvotes: 1
Reputation: 23722
Why not? a UITableView's data source and delegate don't have to be the UITableViewController. Just assign the right object(s) to the table view's dataSource
and delegate
properties, and implement UITableViewDataSource
and UITableViewDelegate
protocols, respectively.
Upvotes: 0