欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS实现展开二级列表效果

程序员文章站 2023-12-16 19:14:04
先来看看效果图 用法(类似uitableview) 初始化xdmulttableview #import "xdmulttableview.h"...

先来看看效果图

IOS实现展开二级列表效果

用法(类似uitableview)

初始化xdmulttableview

#import "xdmulttableview.h"
...
@property(nonatomic, readwrite, strong)xdmulttableview *tableview;

 _tableview = [[xdmulttableview alloc] initwithframe:cgrectmake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
 _tableview.opensectionarray = [nsarray arraywithobjects:@1,@2, nil];
 _tableview.delegate = self;
 _tableview.datasource = self;
 _tableview.backgroundcolor = [uicolor whitecolor];
 [self.view addsubview:_tableview];

实现数据源

- (nsinteger)mtableview:(xdmulttableview *)mtableview numberofrowsinsection:(nsinteger)section{
 return 5;
}

- (xdmulttableviewcell *)mtableview:(xdmulttableview *)mtableview
    cellforrowatindexpath:(nsindexpath *)indexpath{
 static nsstring *cellidentifier = @"cell";
 uitableviewcell *cell = [mtableview dequeuereusablecellwithidentifier:cellidentifier];
 if (cell == nil)
 {
  cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];
 }
 uiview *view = [[uiview alloc] initwithframe:cell.bounds] ;
 view.layer.backgroundcolor = [uicolor whitecolor].cgcolor;
 view.layer.maskstobounds = yes;
 view.layer.borderwidth  = 0.3;
 view.layer.bordercolor  = [uicolor lightgraycolor].cgcolor;

 cell.backgroundview = view;
 cell.selectionstyle = uitableviewcellselectionstylenone;

 return cell;
}

- (nsinteger)numberofsectionsintableview:(xdmulttableview *)mtableview{
 return 6;
}

-(nsstring *)mtableview:(xdmulttableview *)mtableview titleforheaderinsection:(nsinteger)section{
 return @"我是头部";
}

实现代理

- (cgfloat)mtableview:(xdmulttableview *)mtableview heightforrowatindexpath:(nsindexpath *)indexpath{
 return 50;
}

- (cgfloat)mtableview:(xdmulttableview *)mtableview heightforheaderinsection:(nsinteger)section{
 return 40;
}

- (void)mtableview:(xdmulttableview *)mtableview willopenheaderatsection:(nsinteger)section{
 nslog(@"即将展开");
}

- (void)mtableview:(xdmulttableview *)mtableview willcloseheaderatsection:(nsinteger)section{
 nslog(@"即将关闭");
}

- (void)mtableview:(xdmulttableview *)mtableview didselectrowatindexpath:(nsindexpath *)indexpath{
 nslog(@"点击cell");
}

列表展开关闭的实现原理
在section header注册一个手势

//section header view 设置tag值为section
view.tag = section;
uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapheader:)];
 [view addgesturerecognizer:tap];

手势的响应事件

- (void)tapheader:(uitapgesturerecognizer *) tap {
 nsinteger section = tap.view.tag;
 nsnumber *sectionobj = [nsnumber numberwithinteger:section];
 uiimageview *imageview = (uiimageview *)[tap.view viewwithtag:100];
 //_multopensectionarray 用于记录每个 section的展开和关闭状态
 if ([_multopensectionarray containsobject:sectionobj]) {
  nsarray *deletearray = [self builddeleterowwithsection:section];
  [_multopensectionarray removeobject:sectionobj];
  //想关闭的section的所有indexpath
  [_tableview deleterowsatindexpaths:deletearray withrowanimation:uitableviewrowanimationfade];
  [uiview animatewithduration:0.3 animations:^{
   imageview.transform = cgaffinetransformmakerotation(-m_pi/2);
  }];
 }else{
  [_multopensectionarray addobject:sectionobj];
  //想展开的section的所有indexpath
  nsarray *insertarray = [self buildinsertrowwithsection:section];
  [_tableview insertrowsatindexpaths:insertarray withrowanimation:uitableviewrowanimationfade];
  [uiview animatewithduration:0.3 animations:^{
   imageview.transform = cgaffinetransformmakerotation(0);
  }];
 }
}

总结

以上就是ios实现展开二级列表效果的全部内容,希望对大家学习开发ios能有所帮助。

上一篇:

下一篇: