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

3

我在Google上搜了一下,发现iOS 7不再支持Subview。有些人建议创建自定义视图,但我不确定如何做。

这是我的代码,有人可以指导我正确的方向吗?

-(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个回答

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

将真正的tableView作为UIAlertView的子视图的示例:

enter image description here


非常好的解决方案。看起来有些巧妙,但比尝试寻找UIAlertview替代方案并能够添加子视图要好。 - Timuçin

1
你无法这样做。苹果在iOS 7中废弃了向UIAlertView添加任何子视图的功能,我认为这是一个好决定。很多人滥用了UIAlertView
创建自定义视图是一个好主意,但这不是你在代码中写的内容。看起来你又在向UIAlertView添加子视图了。
请参见此处的Alert View UI指南

从链接中得到启发,我看到了一个叫做UIActionSheet的东西。看起来很有趣。 - f00pman
是的,那是人们正在使用的一个解决方案。但是如果您子类化UIViewController来模拟UIAlertView,这也不是一个非常糟糕的想法。在Github上查看是否有人已经完成了它。 - Enrico Susatyo

0

你需要继承UIViewController并使用它的preferredContentSize属性来实现一些“自定义模态”来模仿UIAlertView的布局。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接