iOS基礎(chǔ)-小的Demo--點(diǎn)擊section隱藏或者顯示row

借問漢宮誰得似,可憐飛燕倚新妝!<角金魚>

這個方法不很高明但是總結(jié)一下,加強(qiáng)自己對控件的掌握吧!以后有更好的方法,再來總結(jié).

首先看一下效果.gif
  • 在TableViewController里面實(shí)現(xiàn)
    定義兩個屬性:

// 數(shù)據(jù)源數(shù)組 用來存儲要展示的數(shù)據(jù)
@property (strong, nonatomic) NSMutableArray *dataArray;
// 記錄收縮狀態(tài) 對應(yīng)的把每個區(qū)的展開收縮記錄下來
@property (strong, nonatomic) NSMutableDictionary *dataDic;


這里給一個展示數(shù)據(jù):
// 用一組假的數(shù)據(jù)放到 tableview上面
    
     self.dataArray = [@[@[@"詹姆斯",@"韋德",@"安東尼"],@[@"趙四",@"謝廣坤",@"劉能"],@[@"騷軍",@"蓬勃",@"成龍大哥"],@[@"小三",@"小四",@"小五"]] mutableCopy];
    
// 初始化用于標(biāo)記某一個分區(qū)的section的折疊狀態(tài)的字典
    
      self.dataDic = [NSMutableDictionary dictionary];
      //self.dataDic = [@{@"0":@"1",@"1":@"1",@"2":@"1",@"3":@"1"} mutableCopy];
     // 這里可以給對應(yīng)的區(qū)一個初始狀態(tài),也可以不設(shè)置,因?yàn)闀邳c(diǎn)擊section的方法中進(jìn)行賦值

// 返回分區(qū)數(shù)
```code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return self.dataArray.count;
}```

// 判斷到底是正常顯示row還是row不顯示(返回0行)  這里是 等于 0(字符串類型)的時候 展開
```code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
// 取出字典中的 section 如果是第 0 個分區(qū) 那么就返回該分區(qū)的數(shù)據(jù)
    if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",section]] isEqualToString:@"0"])
    {
        NSLog(@"----------------");
        return [self.dataArray[section] count];
    }else
    {
        NSLog(@"**************");
        return 0;
    }
}```
// 設(shè)置cell上顯示的內(nèi)容
```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;
}```




// 在返回頭視圖的方法里面給每個區(qū)添加一個button
```code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 把分區(qū)的頭視圖設(shè)置成Button
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button.backgroundColor = [UIColor redColor];
// 設(shè)置Button的標(biāo)題作為section的標(biāo)題用
    [button setTitle:[NSString stringWithFormat:@"第 %ld 組",section] forState:(UIControlStateNormal)];
// 設(shè)置點(diǎn)擊事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchDown)];
// 給定tag值用于確定點(diǎn)擊的對象是哪個區(qū)
    button.tag = section + 1000;
    return button;
}```

// 設(shè)置Button的點(diǎn)擊事件
```code
- (void)buttonAction:(UIButton *)sender
{
    NSInteger temp = sender.tag - 1000;
// 修改 每個區(qū)的收縮狀態(tài)  因?yàn)槊看吸c(diǎn)擊后對應(yīng)的狀態(tài)改變 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)];
  }```











最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容