tableview上cell 加载collectionview
2016-06-17 09:44
399 查看
在开发中,或许美工给你的UI设计图 上有很复杂的界面设计,比如一个视图上明显是tableview 但是上面也有collectionview 这时候怎么办,不要慌,下面我教你怎么写。
1首先,创建整体UI界面
- (void)buildUI{
_tableview = [[UITableView
alloc]initWithFrame:CGRectMake(0,
0, VIEW_Width,
VIEW_Height)];
_tableview.delegate =
self;
_tableview.dataSource =
self;
_tableview.rowHeight =
200;
_tableview.backgroundColor = [UIColor
greenColor];
[self.view
addSubview:_tableview];
_collectionView = [[UICollectionView
alloc]initWithFrame:CGRectMake(0,
200,
VIEW_Width,
200) collectionViewLayout:[self
customLayout]];
_collectionView.backgroundColor = [UIColor
redColor];
_collectionView.delegate =
self;
_collectionView.dataSource =
self;
[_collectionView
registerNib:[UINib
nibWithNibName:@"CollectionViewCell"
bundle:nil]
forCellWithReuseIdentifier:@"cell"];
[_tableview
addSubview:_collectionView];
}
2,剩下的就都是实现代理了
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.section ==
0) {
NSLog(@"000000");
}else
if (indexPath.section ==
1){
NSLog(@"index11111");
}else{
NSLog(@"index22");
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *idenfier =
@"cellID";
if (indexPath.section ==
1) {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:idenfier];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:idenfier];
}
cell.imageView.image = [UIImage
imageNamed:@"af4"];
return cell;
}else{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:idenfier];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:idenfier];
}
cell.imageView.image = [UIImage
imageNamed:@"af3"];
return cell;
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return
4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return
10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath{
CollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.item ==
0) {
NSLog(@"0");
}else
if(indexPath.item ==
1){
NSLog(@"1");
}else{
NSLog(@"2");
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return
@"hah";
}
- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return
CGSizeMake(VIEW_Width /
3,120);
}
这样 一个在tableview上cell自定义的collection view就写好了 其实很简单
1首先,创建整体UI界面
- (void)buildUI{
_tableview = [[UITableView
alloc]initWithFrame:CGRectMake(0,
0, VIEW_Width,
VIEW_Height)];
_tableview.delegate =
self;
_tableview.dataSource =
self;
_tableview.rowHeight =
200;
_tableview.backgroundColor = [UIColor
greenColor];
[self.view
addSubview:_tableview];
_collectionView = [[UICollectionView
alloc]initWithFrame:CGRectMake(0,
200,
VIEW_Width,
200) collectionViewLayout:[self
customLayout]];
_collectionView.backgroundColor = [UIColor
redColor];
_collectionView.delegate =
self;
_collectionView.dataSource =
self;
[_collectionView
registerNib:[UINib
nibWithNibName:@"CollectionViewCell"
bundle:nil]
forCellWithReuseIdentifier:@"cell"];
[_tableview
addSubview:_collectionView];
}
2,剩下的就都是实现代理了
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.section ==
0) {
NSLog(@"000000");
}else
if (indexPath.section ==
1){
NSLog(@"index11111");
}else{
NSLog(@"index22");
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *idenfier =
@"cellID";
if (indexPath.section ==
1) {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:idenfier];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:idenfier];
}
cell.imageView.image = [UIImage
imageNamed:@"af4"];
return cell;
}else{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:idenfier];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:idenfier];
}
cell.imageView.image = [UIImage
imageNamed:@"af3"];
return cell;
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return
4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return
10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath{
CollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.item ==
0) {
NSLog(@"0");
}else
if(indexPath.item ==
1){
NSLog(@"1");
}else{
NSLog(@"2");
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return
@"hah";
}
- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return
CGSizeMake(VIEW_Width /
3,120);
}
这样 一个在tableview上cell自定义的collection view就写好了 其实很简单
相关文章推荐
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS App开发中使cell高度自适应的黑魔法详解
- ios8 UITableView设置 setSeparatorInset UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)
- iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
- iOS程序开发中设置UITableView的全屏分隔线的方法(不画线)
- 讲解iOS开发中UITableView列表设计的基本要点
- iOS应用开发中UITableView的分割线的一些设置技巧
- 实例讲解iOS应用开发中使用UITableView创建自定义表格
- iOS App开发中使用及自定义UITableViewCell的教程
- 全面解析iOS应用中自定义UITableViewCell的方法
- iOS App中UITableView左滑出现删除按钮及其cell的重用
- 详解iOS开发中UITableview cell 顶部空白的多种设置方法
- iOS开发中UITableview控件的基本使用及性能优化方法
- 详解iOS开发中UItableview控件的数据刷新功能的实现
- UITableView 实现汽车品牌(demo)
- 改变iOS应用中UITableView的背景颜色与背景图片的方法
- iOS开发之UITableView详解
- 详解iOS App中UITableView的创建与内容刷新
- IOS iPhone 开发 UItableView中的单元格背景渐变
- IOS 9 UITableView整理