我想在iOS8的UIAlertController中構建UITableView。如何使用iOS8在UIAlertController中設置UITableView的位置和大小
我想要下面的效果,這是iOS8的原始組件,彈出alertview和裏面的UITableView。
我嘗試建立的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;
}
代碼效果下方運行效果:
這不是我期待的效果。
有沒有人可以教我如何解決?
非常感謝。
(我不得不使用自動版式)
http://stackoverflow.com/questions/19216477/how -to-add-a-uitableview-into-uialertview-in-ios-7 – 2015-02-23 14:43:07
@ T_77,它僅適用於iOS7 .. – dickfala 2015-02-24 02:27:21