首先是一個視圖實現(xiàn)表格方法
<UITableViewDelegate,UITableViewDataSource>
{
//數(shù)組
UITableView *table;
//? ? 表格元素
NSArray *imgarr,*name;
}
這里是初始化
table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//? ? 代理
table.delegate = self;
table.dataSource = self;
//? ? 加載
[self.view addSubview:table];
//? ? 數(shù)組內(nèi)容
imgarr = @[@"1",@"2",@"3"];
name = @[@"確認添加",@"刪除添加",@"關(guān)閉"];
這里是代理方法
//分區(qū)行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return imgarr.count;
}
//內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
cell.imageView.image = [UIImage imageNamed:imgarr[indexPath.row]];
cell.textLabel.text = name[indexPath.row];
return cell;
}
//點擊行相應(yīng)事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//? ? 判斷第幾行
if (indexPath.row == 0) {
//? ? ? ? 提示框
UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"確認添加"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
//如果等于第二行
else if (indexPath.row == 1){
//? ? ? ? 提示框
UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"刪除添加"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
else if (indexPath.row == 2){
//? ? ? ? 提示框
UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"關(guān)閉"message:@"操作以完成" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
}
調(diào)用在這里實現(xiàn)
//實現(xiàn)按鈕方法
-(void)btnclick{
//初始化內(nèi)容視圖控制器
PopViewController *popview = [[PopViewController alloc]init];
//設(shè)置大小
popview.preferredContentSize = CGSizeMake(150, 140);
// 設(shè)置彈出效果
popview.modalPresentationStyle = UIModalPresentationPopover;
//初始化一個popover
self.pop = popview.popoverPresentationController;
self.pop.delegate = self;
//設(shè)置彈出視圖的顏色
self.pop.backgroundColor = [UIColor whiteColor];
//設(shè)置popover的來源按鈕(以button誰為參照)
self.pop.sourceView = btn;
//設(shè)置彈出視圖的位置(以button誰為參照)
self.pop.sourceRect = btn.bounds;
//箭頭的方向 設(shè)置成UIPopoverArrowDirectionAny 會自動轉(zhuǎn)換方向
self.pop.permittedArrowDirections = UIPopoverArrowDirectionUp;
//模態(tài)出彈框
[self presentViewController:popview animated:YES completion:nil];
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
//點擊蒙版是否消失,默認為yes;
-(BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
//彈框消失時調(diào)用的方法
-(void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
NSLog(@"彈框已經(jīng)消失");
}