2014-09-24 84 views
0

我正在製作我的第一個應用程序。我的應用程序將有一個側面彈出菜單。我已經獲得了用UITableView實現的滑動菜單部分。現在我想用旁邊的文本和圖像填充側邊菜單,當點擊一個圖像時,我想推入另一個視圖控制器。什麼是最好的方法來做到這一點?謝謝如何將項目添加到側邊菜單欄?

+1

可能的重複[在TableView中添加行](http://stackoverflow.com/questions/3496699/add-row-in-a-tableview) – Stephen 2014-09-25 00:12:01

回答

0

正如user132490所說,你可能想要添加行到你的tableView。這裏有一個教程:http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/。這是爲Xcode 5,但仍然會工作。從「添加表格數據」和下面您可能會發現它很有用。事實上,在總結之前,教程教你在每個單元格的文本旁邊添加一個縮略圖。

祝你好運!

0

當您想爲表格添加圖片和文字時,您必須創建一個UITableViewCell子類並聲明圖片視圖和標籤。必須在IB中創建圖像視圖和標籤,創建原型單元格並在其中添加圖像視圖和標籤。

一旦你這樣做了,你必須有一個你的表的內容的數組。它是這樣的:

@interface

@property NSMutableArray *menuArray; 

在你viewDidLoad

NSDictionary *option1 = [NSDictionary dictionaryWithObjectsAndKeys:@"image1.png",@"image",@"Option1",@"desc", nil]; 

    NSDictionary *option2 = [NSDictionary dictionaryWithObjectsAndKeys:@"image2.png",@"image",@"Option2",@"desc", nil]; 

    NSDictionary *option3 = [NSDictionary dictionaryWithObjectsAndKeys:@"image3.png",@"image",@"Option3",@"desc", nil]; 

    NSDictionary *option4 = [NSDictionary dictionaryWithObjectsAndKeys:@"image4.png",@"image",@"Option4",@"desc", nil]; 

    NSDictionary *option5 = [NSDictionary dictionaryWithObjectsAndKeys:@"image5.png",@"image",@"Option5",@"desc", nil]; 


    //assign NSDictionaries to array 
    self.menuArray = [NSMutableArray arrayWithObjects:option1,option2,option3,option4,option5, nil]; 

然後,你必須有表視圖委託方法是這樣的:

#pragma mark Table View delegate methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [menuArray count]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //return row height 
    return 32; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier [email protected]"Cell"; 

    //assign TableCell.h to access custom properties 
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    //get dictionary from menuArray 
    NSDictionary * dict = [self.menuArray objectAtIndex:indexPath.row]; 

    UIImage *imageIcon = [UIImage imageNamed:[dict objectForKey:@"image"]]; 
    //set image 
    [cell.img setImage:imageIcon]; 
    //set the label text 
    cell.label.text = [dict objectForKey:@"desc"]; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //research more about pushing another view controller. 

    //if chosen was option1 
    if (indexPath.row == 0) 
    { 
     NSLog(@"option1"); 
    } 

    //if chosen was option2 
    if (indexPath.row == 1) 
    { 
     NSLog(@"option2"); 
    } 

    //if chosen was option3 
    if (indexPath.row == 2) 
    { 
     NSLog(@"option3"); 
    } 

    // if chosen was option4 
    if(indexPath.row == 3) 
    { 
     NSLog(@"option4"); 
    } 

    // if chosen is option5 
    if(indexPath.row == 4) 
    { 
     NSLog(@"option5"); 
    } 
} 

在推到其他視圖控制器,嘗試研究更多,因爲它是一個lo更多的解釋。試試這個鏈接:https://github.com/ECSlidingViewController/ECSlidingViewController

但是有了這個代碼,你必須能夠填充你的表格,當你選擇一個單元格時會有一個日誌。希望這有助於:)

相關問題