#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *datalist;
@property (nonatomic,strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStyleGrouped];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
//1.獲取文件路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"font" ofType:@"plist"];
//2.通過(guò)路徑加載容器對(duì)象
// _datalist = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:path]];
//(1)讀取文件不可變數(shù)組
NSArray *plist = [NSArray arrayWithContentsOfFile:path];
//(2)初始化可變數(shù)組
_datalist = [[NSMutableArray alloc]init];
//(3)將所有的二級(jí)數(shù)組復(fù)制為可變數(shù)組 并添加到datalist中
for (NSArray *subarr in plist) {
NSMutableArray *ma = [NSMutableArray arrayWithArray:subarr];
[_datalist addObject:ma];
}
}
//進(jìn)入多選模式
- (IBAction)multipleSelect:(UIButton *)sender {
if (sender.selected == YES) {
//1.獲取所有選中的單元格下標(biāo)
NSArray *indexPaths = [_tableView indexPathsForSelectedRows];
//2.刪除數(shù)據(jù)
/**
* 為了防止下標(biāo)越界,所以采用倒序遍歷并刪除
*/
NSLog(@"%@",indexPaths);
for (NSInteger i = indexPaths.count-1; i>=0; i--) {
NSIndexPath *indexP = indexPaths[i];
//(1)獲取section對(duì)應(yīng)的數(shù)組
NSMutableArray *subArr = _datalist[indexP.section];
//(2)
[subArr removeObjectAtIndex:indexP.row];
}
//3.刪除單元格
[_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
sender.selected = !sender.selected;
//在編輯期間允許單元格多選 --> 開(kāi)啟該選項(xiàng) 插入單元格和刪除單個(gè)單元格 功能就不存在了
_tableView.allowsMultipleSelectionDuringEditing = sender.selected;
//表視圖開(kāi)啟編輯模式
[_tableView setEditing:sender.selected animated:YES];
NSLog(@"%@",_datalist);
}
#pragma mark --UITableViewDataSource
//可選方法:返回 組個(gè)數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _datalist.count;//組個(gè)數(shù)
}
//返回 每個(gè)組有多少個(gè)單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//1.從datalist中獲取section下標(biāo)對(duì)應(yīng)的對(duì)象--->小數(shù)組(盛放的NSString*)
NSArray *subArray = _datalist[section];
return subArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.
static NSString *identifier = @"font_cell";
//2.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//3.
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//1.不分組情況
// cell.textLabel.text = [_dataList objectAtIndex:indexPath.row];
//
// cell.textLabel.font = [UIFont fontWithName:[_dataList objectAtIndex:indexPath.row] size:20];
//2.分組
//(1)在datalist中 找到section對(duì)應(yīng)的二級(jí)數(shù)組
NSArray *subArray = _datalist[indexPath.section];
//(2)在二級(jí)數(shù)組中 找到row對(duì)應(yīng)的字符串對(duì)象
cell.textLabel.text = subArray[indexPath.row];
cell.textLabel.font = [UIFont fontWithName:subArray[indexPath.row] size:20];
return cell;
}
@end
屏幕快照 2016-03-03 下午8.43.51.png