脚本宝典收集整理的这篇文章主要介绍了iOS开发之模仿qq通讯录源代码!,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我实现的思路是在tableview的header放置button,然后根据button是否选中来判断是否需要显示那一个section。
首先定义了两个属性
@PRoPErty(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray *buttonsArr;
button数组的定义
-(NSArray *)buttonsArr{
if (_buttonsArr==nil) {
NSMutableArray *tmp=[NSMutableArray array];
for (int i=0; i<6; i++) {
UIButton *button=[UIButton buttonWithType:
UIButtonTypeCustom];
button.tag=333+i;
button.backgroundColor=[UIColor redColor];
[button setTitle:[NSString stringWithFormat:
@"%d",i] forstate:UIControlStateNormal];
button.selected=NO;
[button addTarget:self action:@selector
(buttonAction:) forControlEvents:
UIControlEventTouchUpInside];
[tmp addObject:button];
}
_buttonsArr=[NSArray arrayWithArray:tmp];
}
return _buttonsArr;
}
按钮的点击方法,根据按钮的选中状态刷新tableview
-(void)buttonAction:(UIButton *)button{
button.selected=!button.isSelected;
[self.tableview reloadSections:[NSIndexSet
indexSetWithIndex:button.tag-333] withrowAnimation:
UITableViewRowAnimationFade];
}
tableview的一系列代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)
tableView{
return self.buttonsArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
UIButton *button=self.buttonsArr[section];
if (button.selected==YES) {
return 1;
}else{
return 0;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexpath:(NSIndexPath *)indexPath{
static NSString *str=@"cellid";
UITableViewCell *cell=[tableView
dequeueReusableCellWithIdentifier:str];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseidentifier:str];
cell.textLabel.text=[NSString stringWithFormat:
@"第%li节",indexPath.section];
}
return cell;
}
-(UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section{
UIButton *button=self.buttonsArr[section];
return button;
}
-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150;
}
-(CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section{
return 50;
}
以上是脚本宝典为你收集整理的iOS开发之模仿qq通讯录源代码!全部内容,希望文章能够帮你解决iOS开发之模仿qq通讯录源代码!所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。