gpt4 book ai didi

ios - UITableView 中的 self.delegate=self,有什么问题吗?

转载 作者:行者123 更新时间:2023-11-28 21:28:53 26 4
gpt4 key购买 nike

我的应用程序需要在 iPhone 和 iPad 上通用。对于某些 View ,iPhone 版本与 iPad 版本共享相同的 View (例如,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 的最佳实践是什么?

如果有人能解释一下,在此先感谢。

最佳答案

只要您实现所有必需的方法来填充表格,它就可以正常工作。但它困扰你的原因是因为它设计不佳——它完全模糊了 MVC 设计中的所有界限。您的 tableView 是一个 View 。它应该是愚蠢的,并且出于充分的理由,它使用委托(delegate)来通过简单的适配器插入您拥有的任何模型对象。问问自己,我在 UITableView 的这个子类中是否有任何特定于 View (绘图、外观)的东西?如果不是那么为什么不做一个 tableViewController 或一个小的 NSObject 子类来填充表?最好的。

关于ios - UITableView 中的 self.delegate=self,有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com