2013-09-26 62 views
0

我想添加一個UITableView在我的UIAlertView內,並沒有顯示出來。我只看到標題&消息以及在alertview上的「確定」按鈕。我們如何顯示一個表視圖insiaie UIAlertView

下面是我的代碼,我在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; 
} 
+0

你不知道。 'UIAlertView'從不支持添加子視圖。它在iOS的早期版本中有所作爲,但在iOS 7中完全沒有。您需要自己的自定義警報視圖才能正確執行此操作。 – rmaddy

+0

在WWDC 2013會議之一中,他們提到他們添加了可以添加子視圖的內容視圖,但不幸的是,他們似乎決定取消該視圖。 – rdelmar

+0

它是非常有用的,如果你想使用警報視圖的可用視圖https://github.com/blommegard/SBTableAlert –

回答

4

默認警報並不意味着採取子視圖。(而在iOS6的和早期補充。子視圖可以在黑客攻擊,這並不在iOS7工作)

編寫自定義的UIView,看起來就像一個警告。

如果你不喜歡自己做的話,有很多的實現方法:)例如在cocoacontrols.com

+0

我已經實現了我自己的自定義視圖看起來像UIAlertView。 – Abhinav

0

從iOS的7需要添加這樣

[alertview的setValue:forKey的tableView:@ 「accessoryView的」];

相關問題