iOS 6.0- UIAlertView无法添加子视图。在较低版本中正常工作 [英] iOS 6.0- UIAlertView failed to addSubview. Works fine in lower version
问题描述
这是我在警报视图中显示表格视图的代码。它在iOS 5.1中完美运行。但是在iOS 6.0中,它不会在警报视图中显示表格视图。
UIAlertTableView.h
#import< UIKit / UIKit.h>
@class UIAlertView;
@interface UIAlertTableView:UIAlertView {
//用于装饰
的警报视图UIAlertView * alertView;
//表视图显示
UITableView * tableView;
//表格的高度
int tableHeight;
//表所需空间(包括填充)
int tableExtHeight;
id< UITableViewDataSource>数据源;
id< UITableViewDelegate> tableDelegate;
NSArray *名称;
NSArray *价格;
NSString * priceText;
NSInteger rowsCount;
NSInteger总计;
}
@property(nonatomic,assign)id dataSource;
@property(nonatomic,assign)id tableDelegate;
@property(nonatomic,readonly)UITableView * tableView;
@property(nonatomic,assign)int tableHeight;
@property(nonatomic,assign)NSInteger total;
- (无效)准备;
@end
UIAlertTableView.m
#importUIAlertTableView.h
#define kTablePadding 8.0f
@interface UIAlertView(私有)
- (void)layoutAnimated:(BOOL)fp8;
@end
@implementation UIAlertTableView
@synthesize dataSource;
@synthesize tableDelegate;
@synthesize tableHeight;
@synthesize tableView;
@synthesize total;
- (void)layoutAnimated:(BOOL)fp8 {
[super layoutAnimated:fp8];
[self setFrame:CGRectMake(self.frame.origin.x,self.frame.origin.y - tableExtHeight / 2,self.frame.size.width,self.frame.size.height + tableExtHeight)];
//我们获得最低的非控制视图(即标签),因此我们可以将表视图放在
UIView * lowestView;
int i = 0;
while(![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]){
lowestView = [self.subviews objectAtIndex:i];
i ++;
}
CGFloat tableWidth = 262.0f;
tableView.frame = CGRectMake(11.0f,lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding,tableWidth,tableHeight);
for(UIView * sv in self.subviews){
//将所有控制下移
if([sv isKindOfClass:[UIControl class]]){
sv.frame = CGRectMake(sv.frame.origin.x,sv.frame.origin.y + tableExtHeight,sv.frame.size.width,sv.frame.size.height);
}
}
}
- (无效)显示{
self.total = 0;
[self prepare];
[超级秀];
}
- (NSInteger)tableView:(UITableView *)alerttableView numberOfRowsInSection:(NSInteger)section
{
/ *
代码显示一些app行中的数据
// NSMutableDictionary * rowsUponDict = [AppDelegate productsPFObjectDictionaryAppDelegate];
NSMutableArray * productsNames = [AppDelegate productsPFObjectDictionaryNamesAppDelegate];
// rowsCount = [[rowsUponDict allKeys] count];
rowsCount = [productsNames count];
NSLog(@rowsUponDict count:%d,rowsCount);
返回rowsCount + 1;
* /
返回1;
}
- (UITableViewCell *)tableView:(UITableView *)alerttableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @LazyTableCell;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
/ *
用于显示行中某些应用数据的代码
if(indexPath.row == rowsCount){
cell .textLabel.text = @总金额:;
cell.detailTextLabel.text = [NSString stringWithFormat:@%d,self.total];
}
else {
cell.textLabel.text = [names objectAtIndex:indexPath.row];
priceText = [NSString stringWithFormat:@%@,[price objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [NSString stringWithFormat:@Rs。%@,priceText];
}
* /
返回单元格;
}
- (无效)准备{
if(tableHeight == 0){
tableHeight = 250.0f;
}
/ *
计算一些app数据
NSInteger priceInt;
names = [[NSArray alloc] initWithArray:[AppDelegate productsPFObjectDictionaryNamesAppDelegate]];
NSLog(@姓名:%@,姓名);
prices = [[NSArray alloc] initWithArray:[AppDelegate productsPFObjectDictionaryPricesAppDelegate]];
NSLog(@价格:%@,价格);
for(int i = 0; i< [prices count]; i ++){
priceText = [NSString stringWithFormat:@%@,[price objectAtIndex:i]];
priceInt = [priceText integerValue];
self.total = self.total + priceInt;
NSLog(@tatal:%d,self.total);
}
* /
tableExtHeight = tableHeight + 2 * kTablePadding;
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f,0.0f,0.0f,0.0f)style:UITableViewStylePlain];
tableView.delegate = tableDelegate;
tableView.dataSource = dataSource;
[self addSubview:tableView];
// [self insertSubview:tableView atIndex:0];
[self setNeedsLayout];
}
- (void)dealloc {
[tableView release];
[super dealloc];
}
@end
这是怎么回事我用它
UIAlertTableView * popUpCartItems = [[UIAlertTableView alloc] initWithTitle:@Cart消息:nil delegate :self cancelButtonTitle:@CloseotherButtonTitles:@CheckOut,nil];
popUpCartItems.tableDelegate = popUpCartItems;
popUpCartItems.dataSource = popUpCartItems;
popUpCartItems.tableHeight = 132;
[popUpCartItems show];
非常感谢任何帮助
检查以下链接
https://github.com/simonb/SBTableAlert
我在iOS 5.0和iOS 6.0上都进行过测试,它对我来说非常适合。
您只需要下载该代码并在项目中使用。
希望这对您有帮助!!! / p>
This is my code to show the table view inside alert view. Its working perfectly in iOS 5.1. But in iOS 6.0 it doesnt show the table view inside alert view.
UIAlertTableView.h
#import <UIKit/UIKit.h>
@class UIAlertView;
@interface UIAlertTableView : UIAlertView {
// The Alert View to decorate
UIAlertView *alertView;
// The Table View to display
UITableView *tableView;
// Height of the table
int tableHeight;
// Space the Table requires (incl. padding)
int tableExtHeight;
id<UITableViewDataSource> dataSource;
id<UITableViewDelegate> tableDelegate;
NSArray *names;
NSArray *prices;
NSString *priceText;
NSInteger rowsCount;
NSInteger total;
}
@property (nonatomic, assign) id dataSource;
@property (nonatomic, assign) id tableDelegate;
@property (nonatomic, readonly) UITableView *tableView;
@property (nonatomic, assign) int tableHeight;
@property (nonatomic, assign) NSInteger total;
- (void)prepare;
@end
UIAlertTableView.m
#import "UIAlertTableView.h"
#define kTablePadding 8.0f
@interface UIAlertView (private)
- (void)layoutAnimated:(BOOL)fp8;
@end
@implementation UIAlertTableView
@synthesize dataSource;
@synthesize tableDelegate;
@synthesize tableHeight;
@synthesize tableView;
@synthesize total;
- (void)layoutAnimated:(BOOL)fp8 {
[super layoutAnimated:fp8];
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight/2, self.frame.size.width, self.frame.size.height + tableExtHeight)];
// We get the lowest non-control view (i.e. Labels) so we can place the table view just below
UIView *lowestView;
int i = 0;
while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
lowestView = [self.subviews objectAtIndex:i];
i++;
}
CGFloat tableWidth = 262.0f;
tableView.frame = CGRectMake(11.0f, lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding, tableWidth, tableHeight);
for (UIView *sv in self.subviews) {
// Move all Controls down
if ([sv isKindOfClass:[UIControl class]]) {
sv.frame = CGRectMake(sv.frame.origin.x, sv.frame.origin.y + tableExtHeight, sv.frame.size.width, sv.frame.size.height);
}
}
}
- (void)show{
self.total = 0;
[self prepare];
[super show];
}
- (NSInteger)tableView:(UITableView *)alerttableView numberOfRowsInSection:(NSInteger)section
{
/*
code to show some app data in rows
// NSMutableDictionary *rowsUponDict = [AppDelegate productsPFObjectDictionaryAppDelegate];
NSMutableArray *productsNames = [AppDelegate productsPFObjectDictionaryNamesAppDelegate];
// rowsCount = [[rowsUponDict allKeys] count];
rowsCount = [productsNames count];
NSLog(@"rowsUponDict count: %d",rowsCount);
return rowsCount+1;
*/
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)alerttableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LazyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
/*
code to show some app data in rows
if (indexPath.row == rowsCount) {
cell.textLabel.text = @"Total Amount:";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",self.total];
}
else {
cell.textLabel.text = [names objectAtIndex:indexPath.row];
priceText = [NSString stringWithFormat:@"%@", [prices objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Rs.%@",priceText];
}
*/
return cell;
}
- (void)prepare {
if (tableHeight == 0) {
tableHeight = 250.0f;
}
/*
calculation os some app data
NSInteger priceInt;
names = [[NSArray alloc] initWithArray:[AppDelegate productsPFObjectDictionaryNamesAppDelegate]];
NSLog(@"Names: %@",names);
prices = [[NSArray alloc] initWithArray:[AppDelegate productsPFObjectDictionaryPricesAppDelegate]];
NSLog(@"prices: %@",prices);
for (int i=0; i<[prices count]; i++) {
priceText = [NSString stringWithFormat:@"%@", [prices objectAtIndex:i]];
priceInt = [priceText integerValue];
self.total = self.total + priceInt;
NSLog(@"tatal: %d",self.total);
}
*/
tableExtHeight = tableHeight + 2 * kTablePadding;
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];
tableView.delegate = tableDelegate;
tableView.dataSource = dataSource;
[self addSubview:tableView];
// [self insertSubview:tableView atIndex:0];
[self setNeedsLayout];
}
- (void)dealloc {
[tableView release];
[super dealloc];
}
@end
This is how I use it
UIAlertTableView *popUpCartItems = [[UIAlertTableView alloc] initWithTitle:@"Cart" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"CheckOut",nil];
popUpCartItems.tableDelegate = popUpCartItems;
popUpCartItems.dataSource = popUpCartItems;
popUpCartItems.tableHeight = 132;
[popUpCartItems show];
Any help is greatly appreciated
Check the following link
https://github.com/simonb/SBTableAlert
I tested on both iOS 5.0 and iOS 6.0 and its work perfectly for me.
You just need to do download that code and use in your project.
Hope this will help you !!!
这篇关于iOS 6.0- UIAlertView无法添加子视图。在较低版本中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!