【发布时间】:2016-05-04 10:42:29
【问题描述】:
我的应用需要对 iPhone 和 iPad 通用。对于某些视图,iPhone 版本与 iPad 版本共享相同的视图(例如,tableview)。所以为了更好的重用性,我这样写我的tableview,并在iPhone-controller和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
我觉得这样写不太对,但又说不出来为什么…… 我已经搜索了一段时间,但没有得到任何有用的东西。所以,我的问题是:
- 为什么不能这样写?
- 重用
UITableView的最佳做法是什么?
如果有人能解释一下,请提前感谢。
【问题讨论】:
-
你在哪里获取了tableview的对象?
-
如果您只是因为 iphone 和 ipad 支持而这样做? aur 你需要相同的表格视图用于多种用途吗?如果您只根据尺寸(如平板电脑或手机)执行此操作,则无需执行此操作
-
@AayushKatiyar 在视图控制器中。
-
@trojanfoe 也许你是对的。我只是在尝试。 :)
标签: ios objective-c uitableview code-reuse