標籤:
- 關於UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的傳遞
- 集合嵌套集合的操作
- (void)viewDidLoad
{
[super viewDidLoad];
// 建立一個TabView
self.tabv = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tabv.separatorColor = [UIColor blackColor];
self.tabv.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"44W58PICRtX_1024.jpg"]];
// 擷取資料來源
self.arriOSclass = [NSMutableArray array];
self.arriOSclass = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"iOS_class" ofType:@"plist"]];
// 指定代理和資料來源
self.tabv.delegate = self;
self.tabv.dataSource = self;
// 指定重用儲存格的唯一標識:UITableView的唯一標識註冊UITableViewCell
// - 重用cell而不是本身
[self.tabv registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
// 添加到主視圖
self.navigationItem.title = @"iOS_class";
[self.view addSubview:self.tabv];
}
// 實現協議方法
// 確定顯示的數量(資料來源必須實現_1)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 選中集合
return [self.arriOSclass[section] count];
}
// 擷取顯示的內容(資料來源必須實現_2)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 重用機制:儲存格容量固定,僅僅是更換內容(隊列)
// 儲存一個唯一標識
static NSString *cellIdentity = @"cell";
// 根據tableView的唯一標識和資料分配UITableViewCell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity forIndexPath:indexPath];
// 在UITableViewCell內顯示UITableView對應下標內容
// 使用簡寫
// cell.textLabel.text = [[self.arriOSclass objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
// 選中部分是一個集合,它的row才是字串
cell.textLabel.text = self.arriOSclass[indexPath.section][indexPath.row];
return cell;
}
// 代理方法:為資料分組(TabView的style為group)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.arriOSclass.count;
}
// 為分組命名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"第%lu組", section+1];
}
// 為選中資料添加彈窗
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 先選擇部分(下標)再選擇個體(下標)
NSString *info = self.arriOSclass[indexPath.section][indexPath.row]
;
// 建立彈窗
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:info message:@"確認修改嗎" preferredStyle:UIAlertControllerStyleAlert];
// 添加確認動作
UIAlertAction *alercAOK = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
// 推送到下一頁修改值
PPViewController *ppVc = [PPViewController new];
[self.navigationController pushViewController:ppVc animated:YES];
// 沒有轉場效果?(模態視圖具有)
ppVc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
// 傳遞資料來源中需要修改的索引過去
ppVc.name = info;
// 修改後返回該值:選擇block進行操作
ppVc.ppblock = ^(NSString *name)
{
// 方法的回調:這個方法實際是在第二頁面修改後才會調用該block
// 修改數組的地址
self.arriOSclass[indexPath.section][indexPath.row] = name;
// 修改後重新整理資料返回時才能正常顯示
[self.tabv reloadData];
};
}];
// 添加取消動作
UIAlertAction *alertACancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
// 動作添加到UIAlertController
[alertC addAction:alercAOK];
[alertC addAction:alertACancel];
// UIAlertController添加到當前控制器:即self
[self presentViewController:alertC animated:YES completion:nil];
}
iOS關於UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的傳遞頁面推送