iOS 6.0- UIAlertView failed to addSubview. Works f

2019-08-31 00:15发布

问题:

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

回答1:

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 !!!



回答2:

Try setting a frame for your tableview before adding it to UIAlertView. May be as follows.

tableView.frame = CGRectMake(0, 0, 280, 100);
[self addSubview:tableView];