2010-10-13 47 views

回答

7

添加UISegmentedControls(或UISwitches,我個人建議開/關選項簡單)是很容易的把在使用accessoryView屬性的表格視圖單元格中。

您的視圖控制器(或您用來控制此事的任何對象)必須實現UITableViewDataSource協議。

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {} 
@end 

添加控件的tableView:的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString* const SwitchCellID = @"SwitchCell"; 
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID]; 
    if(aCell == nil) { 
     aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease]; 
     aCell.textLabel.text = [NSString stringWithFormat:@"Option %d", [indexPath row] + 1]; 
     aCell.selectionStyle = UITableViewCellSelectionStyleNone; 
     UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     aCell.accessoryView = switchView; 
     [switchView setOn:YES animated:NO]; 
     [switchView addTarget:self action:@selector(soundSwitched:) forControlEvents:UIControlEventValueChanged]; 
     [switchView release]; 
    } 

    return aCell; 
} 

或者,如果你在分段控制設定,喜歡的東西代替上述UISwitch東西:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"On", @"Off", nil]]; 
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; 
    segmentedControl.frame = CGRectMake(75, 5, 130, 30); 
    segmentedControl.selectedSegmentIndex = 0; 
    [segmentedControl addTarget:self action:@selector(controlSwitched:) forControlEvents:UIControlEventValueChanged]; 
    aCell.accessoryView = segmentedControl; 
    [segmentedControl release];  

我會親自將這些選項放在常規視圖中(可能通過一個選項按鈕進行翻轉過渡,就像很多iPhone應用程序一樣)。我不認爲把它放在一個警報視圖是一個非常好的用戶體驗。但是,我已經寫了幾篇博文,介紹如何將各種視圖放入警報視圖(text field,這很有用,web view,這可以證明不是)。

所以,如果你是在把這個在警報視圖真的死心塌地,你完全可以做到這一點,就像這樣:

- (void) doAlertWithListView { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Preferences" 
                message:@"\n\n\n\n\n\n\n" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

    UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150) 
                 style:UITableViewStyleGrouped] autorelease]; 
    myView.delegate = self; 
    myView.dataSource = self; 
    myView.backgroundColor = [UIColor clearColor]; 
    [alert addSubview:myView]; 

    [alert show]; 
    [alert release];  
} 

它會是這個樣子:

UIAlertView with a UITableView

+0

謝謝pasternack!我會嘗試一下......讓你知道。再次感謝...!! – rockey 2010-10-14 16:20:28

+0

嗨Pasternack!它的工作...!謝謝..:] – rockey 2010-10-15 11:45:15

+0

你必須實現'UITableViewDelegate'到你的'MyViewController'才能設置'myView.delegate = self' – 2013-11-26 12:40:19

0

上面的答案很好,但在iPad上線myView.backgroundColor = [UIColor clearColor]; 似乎並沒有去除桌子周圍的灰色矩形區域。我發現以下工作:tableView.backgroundView.alpha = 0.0;

+0

將tableView.backgroundView.alpha設置爲0不是一個好主意。改爲將視圖設置爲零。 ''tableView.backgroundView = nil;'' – ennalax 2012-07-31 13:14:17

相關問題