Reputation: 846
My app is required to be universal for both iPhone and iPad. For some views, iPhone version shares the same view(for example, a tableview) as iPad version. So for better reusability, I write my tableview like this, and use it in both iPhone-controller and iPad-controller.
.h
@interface NotificationView : UITableView
@end
.m
@interface NotificationView()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation NotificationView
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
self.delegate = self;
self.dataSource = self;
self.rowHeight = 80;
self.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return self;
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
I feel that it's not right to write like this but can't tell why... I've searched for some time, but didn't get anything useful. So, my question is:
UITableView
? Thanks in advance if anyone could explain it.
Upvotes: 0
Views: 574
Reputation: 4728
It will work just fine as long as you implement all the required methods to populate the table. But the reason it is bugging you is because it is poor design - it completely blurs all lines in MVC design. Your tableView is a view. It should be dumb, and it uses delegation to plug into whatever model objects you have with a simple adaptor for good reasons. Ask yourself, is there any view-specific (drawing, appearance) stuff that I'm doing in this subclass of UITableView? If not then why not do a tableViewController or a tiny NSObject subclass to populate the table? Best.
Upvotes: 2