Abhinav
Abhinav

Reputation: 38142

How do we show a table view inside a UIAlertView

I am trying to add a UITableView inside my UIAlertView and its not showing up. I just see title & message along with "OK" button on alertview.

Below is my code and I am running on 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;
}

Upvotes: 0

Views: 3374

Answers (2)

user3699497
user3699497

Reputation:

From iOS 7 you need to add like this

[alertview setValue:tableView forKey:@"accessoryView"];

Upvotes: 0

Daij-Djan
Daij-Djan

Reputation: 50089

The default alert isn't meant to take subviews. (while in ios6 and earlier add. subviews could be hacked in, this does NOT work in iOS7)

write your own custom UIView that looks and acts like an alert.

There's a lot of implementations around if you prefer not to do it yourself :) e.g. on cocoacontrols.com

Upvotes: 4

Related Questions