2015-02-23 35 views
0

我想在iOS8的UIAlertController中構建UITableView。如何使用iOS8在UIAlertController中設置UITableView的位置和大小

我想要下面的效果,這是iOS8的原始組件,彈出alertview和裏面的UITableView。

enter image description here

我嘗試建立的UITableView在UIAlertController,但我不知道怎麼樣設置照片大小和位置。

有沒有人知道如何在UIAlertController中調整UITableView的位置和大小?

我下面的代碼:

@interface ViewController() 
@property (nonatomic,strong) UITableView *tableView; 
@property(strong,nonatomic) NSMutableArray *titleArray; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.titleArray = [[UIFont familyNames] mutableCopy]; 
    for(int i = 0; i < 100; i++) { 
     [self.titleArray addObjectsFromArray:[UIFont familyNames]]; 
    } 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self showUIAlertTable]; 
     }); 

} 

-(void) showUIAlertTable 
{ 
    UIAlertController *alert = [UIAlertController  alertControllerWithTitle:nil 
                    message:@"select network\n\n\n" 
                  preferredStyle:UIAlertControllerStyleAlert]; 


    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds  style:UITableViewStylePlain]; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 

    [alert.view addSubview:self.tableView]; 
    [self presentViewController:alert animated:NO completion:nil]; 

} 


#pragma mark - UITableViewDataSource Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section 
{ 
    return [self.titleArray 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.textLabel.text = [self.titleArray objectAtIndex:indexPath.row]; 
    [cell setNeedsUpdateConstraints]; 

    return cell; 
} 

代碼效果下方運行效果:

enter image description here

這不是我期待的效果。

有沒有人可以教我如何解決?

非常感謝。

(我不得不使用自動版式)

+0

http://stackoverflow.com/questions/19216477/how -to-add-a-uitableview-into-uialertview-in-ios-7 – 2015-02-23 14:43:07

+0

@ T_77,它僅適用於iOS7 .. – dickfala 2015-02-24 02:27:21

回答

1

UIAlertController似乎暴露了非常有限的API,用於一些「行動」的具體情況,「文本框」,並取消按鈕...

如果您想要做任何其他佈局,只需構建一個特定的UIViewController,並自定義其演示文稿。 (Here's an example

所有這一切僅僅是與iOS 8.0及以上兼容的,如果你需要支持的iOS 7,需要其它黑客...

+0

因此,如果我需要支持iOS 7和iOS8,請問我該怎麼辦? – dickfala 2015-02-24 02:27:58

相關問題