国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOSUITableView分組列表

2019-11-14 18:03:59
字體:
來源:轉載
供稿:網友

  UITableView有兩種風格:UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實并沒有本質區別,只是后者按分組樣式顯示前者按照普通樣式顯示而已。今天我們就看看分組的使用:

1、首先我們介紹一下分組的tableView,初始化一個tableView如下

#PRagma mark - 加載表視圖- (void) loadTableView{          _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,20, kWidth, kHeight) style:UITableViewStyleGrouped];        //設置代理    _tableView.delegate=self;    _tableView.dataSource=self;        //設置行高    _tableView.rowHeight=60;    //隱藏分組腳的高度    _tableView.sectionFooterHeight=0;    [self.view addSubview:_tableView];}

2、加載數據,分組數據我們已經在plist文件中定義,加載代碼如下:

#pragma mark - 加載數據- (void)loadData{        NSString * path=[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];    _array=[NSArray arrayWithContentsOfFile:path];    }

3、初始化代理方法

#pragma mark - 設置分組的個數- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{    return _array.count;}#pragma mark - 設置分組的高度- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 40;}#pragma mark - 自定義分組頭- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{           NSDictionary *dic=_array[section];    NSString * title=dic[@"group"];    //1 自定義頭部    UIView * view=[[UIView alloc] init];     view.backgroundColor=[UIColor grayColor];    view.layer.borderWidth=1;    view.layer.borderColor=[UIColor whiteColor].CGColor;        // 2 增加按鈕    UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];    [button setTitle:title forState:UIControlStateNormal];    button.frame=CGRectMake(0, 0, kWidth, 40);    button.tag=section;    [button addTarget:self action:@selector(clickTheGroup:) forControlEvents:UIControlEventTouchUpInside];    [view addSubview:button];        //3 添加左邊的箭頭    UIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5, 40/2.0-30/2.0, 30, 30)];    imageView.image=[UIImage imageNamed:@"disclosure.png"];    imageView.tag=101;    [button addSubview:imageView];    [_headImageView setObject:imageView forKey:@(section)];    return view;}#pragma mark - UITableViewDataSource- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{             return 0;    }

效果圖如下:

  4、我們就可以點擊某個分組進行刷新數據了,通過控制當前分組中數據的個數來達到該效果,由于當前的分組狀態有兩個關閉和打開,因此我們需要定義一個字典來控制狀態,該字典的key為當前分組的索引,值為1 的時候為打開,值為2的時候為關閉。每次點擊的時候我們需要給當前的狀態重新初始化,當前狀態改變的時候對應的分組包含的數據條數置為0

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{        int flag=[_state[@(section)] intValue];    NSDictionary *dic=_array[section];    NSArray * friends=dic[@"friends"];    if(flag){        return friends.count;    }else{        return 0;    }   }

5、刷新需要控制三角號圖標的旋轉,因此我們需要通過動畫,完成當前效果

#pragma mark - 點擊分組信息- (void) clickTheGroup:(UIButton * ) button{    int groupIndex=(int)button.tag;    int flag=0;//用來控制重新實例化按鈕        if([_state[@(groupIndex)] intValue]==0){        [_state setObject:@(1) forKey:@(groupIndex)];        flag=0;    }else{        [_state setObject:@(0) forKey:@(groupIndex)];        flag=1;           }     //刷新當前的分組    NSIndexSet * set=[[NSIndexSet alloc] initWithIndex:groupIndex];        [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];        UIImageView * imageView=_headImageView[@(groupIndex)];        //模擬動畫,每次都重新刷新了因此仿射變化恢復到原始狀態了    if(flag){        imageView.transform=CGAffineTransformRotate(imageView.transform, M_PI_2);    }        [UIView animateWithDuration:0.3 animations:^{                if(flag==0){            imageView.transform=CGAffineTransformMakeRotation( M_PI_2);        }else{            imageView.transform=CGAffineTransformMakeRotation(0);        }    }];   }

完成后效果如下:

動畫瞬間效果

 

作者:杰瑞教育
出處:http://m.survivalescaperooms.com/jerehedu/ 
版權聲明:本文版權歸杰瑞教育技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
技術咨詢:JRedu技術交流
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇沅| 金堂县| 南漳县| 汕头市| 长顺县| 丽水市| 鄯善县| 潢川县| 水城县| 噶尔县| 秦皇岛市| 达拉特旗| 庆安县| 奈曼旗| 平潭县| 武川县| 揭东县| 南昌市| 印江| 宁陵县| 万全县| 筠连县| 奉化市| 随州市| 通道| 沭阳县| 分宜县| 台北市| 镇江市| 城固县| 阿克| 美姑县| 德格县| 汉寿县| 大埔县| 宣城市| 屏山县| 会同县| 丰顺县| 通城县| 舟山市|