My tableView controller is getting huge due to the delegate and data source methods and the fact that I have multiple subclasses of UITableViewCells and different cell layouts for each one. I was looking at this post but it really just goes over how to implement the methods in the dataSource/delegate class, not how to connect it to your view controller.

So I created a class that conforms to the tableView delegate and data source protocols, added a tableView property to be used as a reference to the tableView, and a posts property to be used as the data source array.

@interface SPPostsDataSouceAndDelegate : NSObject <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *posts;

@end

I cut my delegate and data source methods from my tableView controller into the implementation of that class. then in my tableView controller I added a property for that dataSource and delegate class.

@property (nonatomic) SPPostsDataSouceAndDelegate *postsDataSourceAndDelegate;

and in view did load set the data source and delegate as that property, and set that properties tableView and posts properties to that of my view controller.

self.tableView.delegate = self.postsDataSourceAndDelegate;
self.tableView.dataSource = self.postsDataSourceAndDelegate;
self.postsDataSourceAndDelegate.tableView = self.tableView;
self.postsDataSourceAndDelegate.posts = self.posts; 

However it does not work. Is there anything im missing? Like I said I tried to follow along with the answer I linked but it doesnt really explain this part, and everything else I found doesnt really have anything on what to do once you actually create the separate class so im sort of lost here.

Related posts

Recent Viewed