網頁

2013年2月26日 星期二

Muti-View 多重視圖 與 Master-Detail app (MySecretRecipe)

[iphone app 開發]

之前的文章,針對的是一個"RD很忙的app" 作討論,從這篇開始,

將跨入多個view 的app 討論,我將學習國外的知名app Drink Mixer 和 iphone 中的 mail 和

Contacts 來繼續更進階的iphone app 討論。

我命名為 : MySecretRecipe。
目的:很簡單,讓使用者能自行輸入自己最拿手的食譜,原料,和作法,可以隨著iphone帶著走,無論是到異地作菜,或是去市場買菜,直接拿出來管理,編輯。
可能會使用的人: 新手廚師,一般做菜的人,負責採買的人。


1.一開始先創一個Master-Detail app 的project.
(以前的Navigation-based app 類型被這個取代了)

2. 第一個目標,我們希望在中間這個table view 的地方,先預設 show 出三到菜的名稱。



3.接著,當你點下其中一道菜,app會往下show 出那到菜的細節和作法。

就是這樣。




好,所以這篇先來達到目標1.

在app launch 時,秀出預設的三到菜名稱,這將會使用到table view。





好,知道目標是什麼了(就是上圖)很簡單,現在的任務就是,要怎麼把那三道菜的名稱,
show 在原本你剛開好了Master-Detail application 的中間那個區域。


1. 在這三個預設的view 中,中間那個如果你用滑鼠點一下,你會發現,右邊的doc 會說,它是 UITableView 的 控制元件,所以,你應該想的道,UITableView 這個控制元件,在apple的說明檔中,應該有些必要的資訊,和該餵給它的資料(也就是三道菜名),它應該就會乖乖的秀出來了吧。再者,想要秀多少data, 應該是會找到它的datasource function 吧。

ok, 順著這個猜想,去 viewController.m 裡看看吧,找到一個已經產生好的,


#pragma mark - Table View 

這個tag 是給compiler 看的,往下應該就是table view 的相關必作function.

果然,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

這兩個都可以在apple doc 中找到解釋。





UITableView Class Reference

numberOfSectionsInTableView:
Asks the data source to return the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Parameters
tableView
An object representing the table view requesting this information.
Return Value
The number of sections in tableView. The default value is 1.






tableView:numberOfRowsInSection:
Tells the data source to return the number of rows in a given section of a table view. (required)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Parameters
tableView
The table-view object requesting this information.
section
An index number identifying a section in tableView.
Return Value
The number of rows in section.



好,現在你知道,第一個function 是要回答待會這個table view 要show 幾個section, 這邊我們default 是一個section, 那如果你改成2 呢? 它就會把畫面上下對切,變成兩個section, 當然,你也知道,到時候分別要餵給上下兩個section 資料,以便show 在ui 上。所以,這個function 我們一個字也不動,讓他自己return 1;


tableView:numberOfRowsInSection 而這個function, 它的解釋就是有多少row要show (這邊就是我們預設秀出三道菜名,那應該就是三),所以你也可以預料道,應該會有一個陣列,或是其他資料格式,裝著要秀的data, 然後再這個function 回傳到底有多少資料要show。


2. 好,所以你知道,你需要一個結構類似陣列,先硬裝三道菜名,然後把上面兩個function 該得到的資訊,從那個陣列裡取得,讓UIshow 的對。



ex:


MySecretRecipe2MasterViewController.h
@interface MySecretRecipe2MasterViewController : UITableViewController{

    NSMutableArray* recipes_;

}

@property (nonatomic,retain) NSMutableArray *recipes;



.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return _objects.count;
    return [self.recipes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    //NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [self.recipes objectAtIndex:indexPath.row];
    return cell;
}







3. 當然,這個recipes 的陣列,需要init 塞一些值在 viewDidLoad.


- (void)viewDidLoad
{


    recipes_ = [[NSMutableArray alloc]initWithObjects:@"白酒蛤蠣義大利麵",@"香腸蛋炒飯",@"泰式炒麵", nil];


......


Build 過,執行後就出現我們要的啦,不過很明顯,你點下去香腸蛋炒飯雖然頁面會
往右滑,但是裡面什麼資訊都沒有......。

在填入香腸蛋炒飯的原料和作法之前,光這裡就有一些問題不太合理....

=> 菜單竟然要hard code 在viewDidLoad !!

我們將處理這個問題先。










沒有留言:

張貼留言