我们如何在UIAlertView中显示表视图

问题描述 投票:0回答:2

我试图在我的UIAlertView中添加一个UITableView,它没有显示出来。我只是在alertview上看到标题和消息以及“确定”按钮。

下面是我的代码,我在iOS 7上运行。

- (void)showDataReceivedAlert {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Received Data" message:@"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(10, 40, 264, 120)];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor clearColor];
    [alert addSubview:table];
    [alert show];
}


- (NSInteger)tableView:(UITableView *)iTableView numberOfRowsInSection:(NSInteger)iSection {
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    static NSString *identifier = @"iBeaconDataCell";
    UITableViewCell *cell = [iTableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    NSString *cellText;
    NSString *cellLabelText;

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedObject = [userDefaults objectForKey:@"data"];
    MyObj *myData = (MyObj *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

    switch (iIndexPath.row) {
        case 0:
            cellText = @"Data 1";
            cellLabelText = [NSString stringWithFormat:@"%d", myData.data1];
            break;
        case 1:
            cellText = @"Data 2";
            cellLabelText = [NSString stringWithFormat:@"%d", myData.data2];
            break;
        case 2:
            cellText = @"Data 3";
            cellLabelText = [NSString stringWithFormat:@"%d", myData.data3];
            break;
        case 3:
            cellText = @"Data 4";
            cellLabelText = [NSString stringWithFormat:@"%d", myData.data4];
            break;
        case 4:
            cellText = @"Data 5";
            cellLabelText = [NSString stringWithFormat:@"%f", myData.data5];
            break;
        case 5:
            cellText = @"Data 6";
            cellLabelText = myData.data6;
            break;

        default:
            break;
    }

    cell.textLabel.text = cellText;

    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 44.0)];
    cellLabel.text = cellLabelText;
    cell.accessoryView = cellLabel;

    return cell;
}
ios objective-c cocoa-touch uitableview uialertview
2个回答
4
投票

默认警报不是为了获取子视图。 (在ios6和更早版本中添加。子视图可能被黑客入侵,这在iOS7中不起作用)

编写自己的自定义UIView,其外观和行为类似于警报。

如果您不想自己做,那么有很多实现:)例如在cocoacontrols.com上


0
投票

从iOS 7开始,您需要添加这样的内容

[alertview setValue:tableView forKey:@“accessoryView”];

© www.soinside.com 2019 - 2024. All rights reserved.