如何在iOS 7中将UITableView添加到UIAlertView中

时间:2013-10-07 03:04:13

标签: uitableview ios7 uialertview

有些谷歌搜索看到iOS 7不再支持子视图。

有些人建议创建自定义视图,但我不知道我该怎么做。

这是我的代码,有人能指出我正确的方向吗?

-(IBAction)click_select_fruit_type
{
select_dialog = [[[UIAlertView alloc] init] retain];
[select_dialog setDelegate:self];
[select_dialog setTitle:@"Fruit Type"];
[select_dialog setMessage:@"\n\n\n\n"];
[select_dialog addButtonWithTitle:@"Cancel"];

idType_table = [[UITableView alloc]initWithFrame:CGRectMake(20, 45, 245, 90)];
idType_table.delegate = self;
idType_table.dataSource = self;
[select_dialog addSubview:idType_table];

[idType_table reloadData];

[select_dialog show];
[select_dialog release];

}

3 个答案:

答案 0 :(得分:13)

您可以在iOS7的标准提醒视图中将 accessoryView 更改为任何自己的 customContentView

[alertView setValue:customContentView forKey:@"accessoryView"];

请注意,您必须在 [alertView show] 之前调用此方法。

最简单的说明示例:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:@"accessoryView"];
[av show];

enter image description here

Real tableView 作为 UIAlertView 示例的子视图:

enter image description here

答案 1 :(得分:1)

你做不到。 Apple不赞成在iOS 7中向UIAlertView添加任何子视图的功能。我认为这是一个很好的决定。很多人滥用UIAlertView

创建自定义视图是一个好主意,但这不是您在代码中编写的内容。您似乎要再次向UIAlertView添加子视图。

请参阅Alert View UI guide here

答案 2 :(得分:0)

你必须继承UIViewController并使用其preferredContentSize属性来做一些模仿UIAlertView布局的'自定义模态'