美文网首页
tableview好友列表的实现

tableview好友列表的实现

作者: 张家杰仔 | 来源:发表于2017-04-05 10:28 被阅读14次

    收缩

    9596797.png

    展开

    11311111.png
    tableview 的section的协议方法 (避免重用问题 可以 自定义一个类继承于UITableViewHeaderFooterView 给他设置属性,参照tableview的cell一样赋值)
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        /*
         标题label + 分割线 + button
         */
        UITableViewHeaderFooterView *heaerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEADERID];
        
        if (!heaerView) {
            heaerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:HEADERID];
        }
            UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kWidth - 64, 64)];
            //标题赋值
            titleLabel.text = _dataSource[section][0][@"techniquesType"];
            titleLabel.font = [UIFont boldSystemFontOfSize:22];
            
            UILabel *linelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 63, kWidth - 20, 1)];
            linelabel.backgroundColor = [UIColor lightGrayColor];
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(kWidth - 54, 10, 44, 43)];
            button.backgroundColor = [UIColor redColor];
            button.tag = 100 + section;
            //判断里面有值与否 来控制button的状态
            if ([_dict objectForKey:[NSString stringWithFormat:@"%ld",section]]) {
                button.selected = YES;
            }else{
                button.selected = NO;
            }
          
            [button setImage:[UIImage imageNamed:@"inter_open"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"inter_close"] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(action_sectionbutton:) forControlEvents:UIControlEventTouchUpInside];
            [heaerView addSubview:titleLabel];
            [heaerView addSubview:linelabel];
            [heaerView addSubview:button];
            heaerView.contentView.backgroundColor = [UIColor whiteColor];
            
        return heaerView;
    }
    收缩展开的点击事件
    - (void)action_sectionbutton:(UIButton *)sender{
        if (!_dict) {
            _dict = [NSMutableDictionary dictionary];
        }
        NSString *key= [NSString stringWithFormat:@"%ld",sender.tag - 100];
        if (![_dict objectForKey:key]) {
            [_dict setObject:@"1" forKey:key];
        }else{
            [_dict removeObjectForKey:key];
        }
     //刷新某一个section
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:sender.tag - 100] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    tableview的section高度的协议方法
    //row的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        if ([_dict objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
             return 44.0;
        }else{
            return 0;
        }
    }
    

    相关文章

      网友评论

          本文标题:tableview好友列表的实现

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