0

这是我在警报视图中显示表格视图的代码。它在 iOS 5.1 中完美运行。但在 iOS 6.0 中,它不会在警报视图中显示表格视图。

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

这就是我使用它的方式

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];

任何帮助是极大的赞赏

4

2 回答 2

2

检查以下链接

https://github.com/simonb/SBTableAlert

我在 iOS 5.0 和 iOS 6.0 上都进行了测试,它非常适合我。

您只需要下载该代码并在您的项目中使用。

希望对你有帮助 !!!

于 2012-11-09T05:39:05.653 回答
0

在将它添加到 UIAlertView 之前,尝试为您的 tableview 设置一个框架。可能如下。

tableView.frame = CGRectMake(0, 0, 280, 100);
[self addSubview:tableView];
于 2012-11-08T11:12:00.643 回答