美文网首页iOS基础扩展自己尝试等装一下天生不是作曲家
iOS基础-小的Demo--点击section隐藏或者显示row

iOS基础-小的Demo--点击section隐藏或者显示row

作者: 云之君兮鹏 | 来源:发表于2016-06-30 21:39 被阅读2555次
借问汉宫谁得似,可怜飞燕倚新妆!<角金鱼>

这个方法不很高明但是总结一下,加强自己对控件的掌握吧!以后有更好的方法,再来总结.

首先看一下效果.gif
  • 在TableViewController里面实现
    定义两个属性:

// 数据源数组 用来存储要展示的数据
@property (strong, nonatomic) NSMutableArray *dataArray;
// 记录收缩状态 对应的把每个区的展开收缩记录下来
@property (strong, nonatomic) NSMutableDictionary *dataDic;


这里给一个展示数据:
// 用一组假的数据放到 tableview上面
    
     self.dataArray = [@[@[@"詹姆斯",@"韦德",@"安东尼"],@[@"赵四",@"谢广坤",@"刘能"],@[@"骚军",@"蓬勃",@"成龙大哥"],@[@"小三",@"小四",@"小五"]] mutableCopy];
    
// 初始化用于标记某一个分区的section的折叠状态的字典
    
      self.dataDic = [NSMutableDictionary dictionary];
      //self.dataDic = [@{@"0":@"1",@"1":@"1",@"2":@"1",@"3":@"1"} mutableCopy];
     // 这里可以给对应的区一个初始状态,也可以不设置,因为会在点击section的方法中进行赋值

// 返回分区数
```code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return self.dataArray.count;
}```

// 判断到底是正常显示row还是row不显示(返回0行)  这里是 等于 0(字符串类型)的时候 展开
```code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
// 取出字典中的 section 如果是第 0 个分区 那么就返回该分区的数据
    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",section]] isEqualToString:@"0"])
    {
        NSLog(@"----------------");
        return [self.dataArray[section] count];
    }else
    {
        NSLog(@"**************");
        return 0;
    }
}```
// 设置cell上显示的内容
```code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

cell.backgroundColor = [UIColor colorWithRed:(arc4random()%173)/346.0 + 0.5 green:(arc4random()%173)/346.0 + 0.5  blue:(arc4random()%173)/346.0 + 0.5  alpha: 1];

cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];

return cell;
}```




// 在返回头视图的方法里面给每个区添加一个button
```code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 把分区的头视图设置成Button
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button.backgroundColor = [UIColor redColor];
// 设置Button的标题作为section的标题用
    [button setTitle:[NSString stringWithFormat:@"第 %ld 组",section] forState:(UIControlStateNormal)];
// 设置点击事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchDown)];
// 给定tag值用于确定点击的对象是哪个区
    button.tag = section + 1000;
    return button;
}```

// 设置Button的点击事件
```code
- (void)buttonAction:(UIButton *)sender
{
    NSInteger temp = sender.tag - 1000;
// 修改 每个区的收缩状态  因为每次点击后对应的状态改变 temp代表是哪个section
    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",temp]]isEqualToString:@"0"] )
    {
        [self.dataDic setObject:@"1" forKey:[NSString stringWithFormat:@"%ld",temp]];
    }else
{  
     [self.dataDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",temp]];
}
    // 更新 section
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
  }```











相关文章

网友评论

  • 风雨落山岚:有demo吗,紧急需要:smile:
    云之君兮鹏:@风雨落山岚 不好意思现在不做iOS好久了,,,,,
  • 松少:你这有没有Demo啊
    松少:不能多个cell,同时存在变大后的cell
    松少:@云之君兮鹏嗯嗯
    目前我实现到点击变大,但是点击一个另一个又回到初始高度了
    云之君兮鹏:@松少 时间太久了 我找找看! 您需要吗?
  • 井湾村夫:很不错哦
    云之君兮鹏: @咚咚响 谢谢
  • 夏离:就是第一组的詹姆斯没显示,从赵四开始显示的,从1开始的,没有0那一组:unamused:
    云之君兮鹏:@云之君兮鹏 你添加一个头部视图高度就行了
    云之君兮鹏:@夏离 可以把你的工程发给我,一起学习一下.yunpengdai@outlook.com
  • 夏离:为啥我的第一个section不显示呢
    云之君兮鹏:@夏离 具体怎么样的了?
  • 华之曦:哎哟,不错。
  • 起床干活养老婆:很好,简洁明了。不会的一看就懂了
    云之君兮鹏: @起床干活养老婆 谢谢
  • CYC666:点击单元格就有代理方法,可以不用添加button,是不是?
    云之君兮鹏: @CYC666 这个是section不是点击cell方法

本文标题:iOS基础-小的Demo--点击section隐藏或者显示row

本文链接:https://www.haomeiwen.com/subject/karfjttx.html