如何解决Obj-C- UITableViewCell内的UICollectionView的数据源?
我在ViewController(DashboardViewController)内部有一个TableView(self.tableView)。在此tableview的内部,有一个包含UICollectionView的自定义单元。
一切正常,但是为了填充UICollectionView,我目前正在UITableViewCell内调用数据,我不想这样做,因为每次上下滚动时都会调用数据。
是否有可能使UICollectionView的数据源在DashboardViewController内成为NSMutableArray,而不必在其所在的UITableViewCell内填充NSMutableArray?
我现在的设置:
UITableViewCell.h
git add
UITableViewCell.m
@property (weak,nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong,nonatomic) NSMutableArray *clientsWeek;
解决方法
在正确设置 CellReuseIdentifier 的前提下。
当然,您可以选择获取DashboardViewController
中的所有数据,包括UICollectionView
的数据源。在这里,您需要合并数据。 tableview的数据源必须是一个数组,因此UICollectionView的数据源可以放在数组中。像这样
[
{
"UITableViewCellData":{},"UICollectionViewData":{}
},...
]
当然,您也可以使用数据模型对象进行替换。
然后,您需要向单元公开用于接收数据源的方法,您可以在其中刷新UICollectionView。
UITableViewCell.h
- (void)cellWithDataSource:(NSDictionary *)dataSource;
UITableViewCell.m
- (void)cellWithDataSource:(NSDictionary *)dataSource{
NSDictionary *tableViewDataSource = dataSource[@"UITableViewCellData"];
NSDictionary *collectionViewDataSource = dataSource[@"UICollectionViewData"];
...
}
DashboardViewController
将数据传递到单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
// first obtains the datasource of each cell,and passes it to each cell
[cell cellWithDataSource:datasource];
return cell;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。