How to add UITableview in UIAlertController in iOS8?

陌路散爱 提交于 2019-12-13 15:00:34

问题


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 ?

回答1:


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];
    }
}

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];
}



回答2:


Courtesy StackOverFlow users i was able to do this task... here is my code...

 UIViewController *controller = [[UIViewController alloc]init];
UITableView *alertTableView;
if (array.count < 4) {
    CGRect rect = CGRectMake(0, 0, 272, 100);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 100)];
}
else if (array.count < 6){
    CGRect rect = CGRectMake(0, 0, 272, 150);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 150)];
}
else if (array.count < 8){
    CGRect rect = CGRectMake(0, 0, 272, 200);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 200)];
}
else{
    CGRect rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 250)];
}

alertTableView.delegate = self;
alertTableView.dataSource = self;
alertTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[alertTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[alertTableView setTag:kAlertTableViewTag];
[controller.view addSubview:alertTableView];
[controller.view bringSubviewToFront:alertTableView];
[controller.view setUserInteractionEnabled:YES];
[alertTableView setUserInteractionEnabled:YES];
[alertTableView setAllowsSelection:YES];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController setValue:controller forKey:@"contentViewController"];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];



来源:https://stackoverflow.com/questions/27635814/how-to-add-uitableview-in-uialertcontroller-in-ios8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!