如何在iOS8中的UIAlertController中添加UITableview? [英] How to add UITableview in UIAlertController in iOS8?

查看:182
本文介绍了如何在iOS8中的UIAlertController中添加UITableview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自定义视图中直到iOS7,我们可以将tableview置于警告状态,如下图所示。





$
但在iOS8中UITableview无效。我可以把它放在tableview中,但tableview不滚动,也没有检测到任何单元格上的任何点击。







那么,我该如何实现这种类型的AlertController?

Till iOS7 in Custom view we can put tableview in alert as per below picture.



But in iOS8 UITableview is not working. I can put it in tableview but the tableview is not scrolling and not detecting any click on any cell.



So, how do I implement this type of AlertController ?

推荐答案

请使用自定义视图和动画尝试以下代码

Please try below code with custom view and animations

Test.h

#import <UIKit/UIKit.h>

@interface Test : UIView <UITableViewDataSource,UITableViewDelegate>
{
    UITableView *table;
}

- (id)initWithFrame:(CGRect)frame Array:(NSArray *)ArrayValue;
- (void)showInView:(UIView *)aView animated:(BOOL)animated;
- (void)fadeOut;

@end

Test.m

- (id)initWithFrame:(CGRect)frame Array:(NSArray *)ArrayValue
{
    if (self = [super initWithFrame:frame])
    {
        aryType = ArrayValue;
        table = [[UITableView alloc] initWithFrame:CGRectMake(15, 0, 320, 380) style:UITableViewStylePlain];
        table.dataSource = self;
        table.delegate = self;
        [table setSeparatorInset:UIEdgeInsetsZero];
        table.showsVerticalScrollIndicator = YES;
        [self addSubview:table];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return aryType.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    cell.textLabel.text = [aryType objectAtIndex:(indexPath.row)];
    cell.textLabel.font = [UIFont fontWithName:@"Avenir Next" size:16];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"select");
}

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
    [aView addSubview:self];
    if (animated)
    {
        [self fadeIn];
    }
}

最后从你的viewcontroller调用此视图如下代码

Viewcontroller.m

At last from your viewcontroller call this view as per below code
Viewcontroller.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *aryType = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
    [self showPopUpWithTitle:@"Title" arrOptions:aryType xy:CGPointMake(16, 150) size:CGSizeMake(287, 134)];
}

- (void)showPopUpWithTitle:(NSString *)popupTitle arrOptions:(NSArray *)arrOptions xy:(CGPoint)point size:(CGSize)size
{
    testView = [[Test alloc] initWithFrame:CGRectMake(15, point.y, size.width, 200.0) Array:arrOptions];
    [testView showInView:self.view animated:YES];
}

这篇关于如何在iOS8中的UIAlertController中添加UITableview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆