UITableView programmatically create delegate object?
I have a question regarding setting up a custom delegate class for use with UITableView. What I have done is as follows:
- Setup a new class (in sperate *.h and *.m files for the class)
- Conformed that new class to the
<UITableViewDelegate, UITableViewDataSource>
protocols - Added the required methods.
- Created a pointer to the new object using @property and IBOutlet.
- In InterfaceBuilder created and assigned an object template to my new class
- Assigned the dataSource and delegate connections.
This all works fine. My question is if I don't want to use interfaceBuilder to setup and instantiate my new开发者_高级运维 delegate class how do I go about doing that in Xcode instead? More specifically how would I:
- Instantiate the delegate class, would that be created / owned by the controller?
- Set the dataSource and delegate connections?
- What is the best way of doing this?
In your view controller, create a new instance of the delegate class and assign it to your property, make sure to remove IBOutlet
MyDelegateClass * delegateClass = [[MyDelegateClass alloc] init];
[self setMyProperty:delegateClass];
[delegateClass release];
You can set set your tableview's delegate and datasource by doing:
[myTableView setDelegate:self.myProperty];
[myTableView setDatasource:self.myProperty];
You should probably put all this in viewDidLoad
精彩评论