2011-08-03 67 views
0

我正在製作一個具有tableview和DetailViewController的應用程序。我使用Apple的「MultipleDetailViews」代碼作爲啓動板。我想讓這個應用程序在分割視圖中有大約100行,並且我希望細節視圖對於每一行都進行更改(閱讀100個視圖)。我怎樣才能使用界面生成器,但不生成100個類文件並重復更改名稱?如何在界面構建器中快速製作大量視圖控制器?

目前我唯一的方法是手動創建單獨的視圖控制器(與類文件)。然而,這非常激動人心。

反正我可以用一個DetailViewController,添加幾個意見,其界面生成器內,推動每個視圖,當我選擇在tableview中的行。

在我想補充一個背景圖像和含有不同的聲音(每行的觀點將有三個獨特的聲音)三個按鈕每個視圖。我怎樣才能創建三個IBActions並根據選擇的行更改聲音文件路徑?

有沒有時間有效的方法來做我想問的問題?

回答

2

100個視圖控制器類?這不好。

單個視圖控制器類的100個實例?我不希望。

讓我們來看看你用2描述的行爲,只是2.你有一個控制器爲你的表視圖和一個控制器爲你的詳細信息視圖。而已。

當您在表視圖中選擇一行時,將該行索引傳遞給詳細信息視圖控制器,併爲詳細信息視圖控制器提供一種基於該行加載正確圖像和聲音的方法。

這可能來自您的圖像和聲音資源('background0','background1',...)的命名約定,或者它可能來自某個配置文件,它定義了每行的背景圖像和聲音包含字典數組的plist:[{background:「moon.png」,firstSound:「clown.mp3」,secondSound:「moose.mp3」,thirdSound:「water.mp3},{...},.. 。])。

1

這聽起來像你的每一個細節的意見將是非常相似,你可以創建一個單一的UIViewController實例,每個小區被竊聽的時間重新配置。下面是一個示例,說明如何修改MultipleDetailViews項目,以根據選定的行更改單個UIViewController實例的背景顏色。

static const NSUInteger kRowCount = 100; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*kRowCount); 
    // Create an array of colors to cycle through 
    self.colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; 
} 

#pramga mark - UITableViewDataSource methods 

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { 
    return kRowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Dequeue or create cell 
    cell.textLabel.text = [NSString stringWithFormat:@"View Controller #%d", indexPath.row + 1]; 
    return cell; 
} 

#pramga mark - UITableViewDataDelegate methods 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Don't create a new view controller, just reconfigure the on that is already displayed 
    DetailViewController *dvc = [self.splitViewController.viewControllers objectAtIndex:1]; 
    dvc.view.backgroundColor = [colors objectAtIndex:(indexPath.row % [colors count])]; 
} 
相關問題