欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)中使用Picker View實(shí)現(xiàn)一個(gè)點(diǎn)菜應(yīng)用的UI示例

 更新時(shí)間:2016年01月16日 09:20:24   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中使用Picker View實(shí)現(xiàn)一個(gè)點(diǎn)菜應(yīng)用的UI示例,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

一、實(shí)現(xiàn)效果

  說明:點(diǎn)擊隨機(jī)按鈕,能夠自動(dòng)選取,下方數(shù)據(jù)自動(dòng)刷新。

201611691958282.png (320×502)

二、實(shí)現(xiàn)思路

1.picker view的有默認(rèn)高度為162,不可修改。
2.顯示數(shù)據(jù),需要設(shè)置數(shù)據(jù)源,也有兩種方式(成為數(shù)據(jù)源,遵守協(xié)議)
3.實(shí)現(xiàn)數(shù)據(jù)源里面的兩個(gè)方法
1)返回一共有多少列
2)在這一列中一共有多少行
4.通過代理告訴它那一列的哪一行顯示哪些數(shù)據(jù)(設(shè)置其代理為控制器)
5.使用懶加載,加載所有的食物
6.完成基本數(shù)據(jù)的展示(列,行,內(nèi)容)
7.自動(dòng)更新選中的食物信息。(使用一個(gè)大的view,上面放6個(gè)label)
1)給3個(gè)lab賦值,添加三個(gè)屬性(水果,主菜,飲料)
2)監(jiān)聽選中了哪一行(監(jiān)聽有兩種思想,一個(gè)是代理,一個(gè)是通知),先查看有沒有代理的方法(didselectRow)這個(gè)方法當(dāng)選中了某一行的的時(shí)候調(diào)用,會(huì)將選中的列號(hào)和行號(hào)當(dāng)做參數(shù)傳入進(jìn)去。能夠獲取到對(duì)應(yīng)的列號(hào)和行號(hào)。
3)完成選中時(shí)調(diào)用的監(jiān)聽方法
4)在viewdidload里面設(shè)置默認(rèn)選中的內(nèi)容,設(shè)置為[0][1]
5)提高可擴(kuò)展性(手動(dòng)的調(diào)用那幾行-使用一個(gè)for循環(huán))
8.隨機(jī)功能的實(shí)現(xiàn)
1)怎么讓代碼選中某一行(selectrow),調(diào)用該方法可以指定讓它滾動(dòng)到那一列的哪一行
2)實(shí)現(xiàn)頭部的功能(使用一個(gè)大的uiview,里面放兩個(gè)子控件)
3)設(shè)置高度44,怎么讓隨機(jī)按鈕的位置居中?可以設(shè)置它的高度為44,最大的Y值為64。
4)設(shè)置隨機(jī)按鈕的點(diǎn)擊事件randomFood,讓pickerview主動(dòng)選中某一行。
5)生成隨機(jī)數(shù)的方法(生成隨機(jī)數(shù)的限制,不超過當(dāng)前的總數(shù))
6)缺點(diǎn),將來數(shù)據(jù)改變之后,會(huì)報(bào)錯(cuò)(模于幾)[self.foods[0] count]?為什么不用簡(jiǎn)寫 點(diǎn)語法?(切記要記?。?br /> 7)隨機(jī)數(shù)的處理不嚴(yán)謹(jǐn),有的時(shí)候生成的隨機(jī)數(shù)可能是相等的,那么這樣的話列就不會(huì)滾動(dòng),獲取到對(duì)應(yīng)列的數(shù)據(jù)總數(shù),如何拿到上一次產(chǎn)生的隨機(jī)值(也就是當(dāng)前選中的行),比較上一次的行號(hào)和當(dāng)前生成的隨機(jī)數(shù)是否相同,如果相同則重寫生成
9.解決另外一個(gè)問題,下面的數(shù)據(jù)隨機(jī)刷新失效了,通過代碼選中某一行。
 
三、實(shí)現(xiàn)代碼示例
1.項(xiàng)目文檔結(jié)構(gòu)和storyboard文件

201611692023598.png (853×306)

storyboard文件大的界面設(shè)置:

201611692040383.png (451×580)

2.代碼示例
主控制器文件代碼:

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  06-簡(jiǎn)單選菜系統(tǒng)的實(shí)現(xiàn)
//
//  Created by apple on 14-6-5.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

//遵守?cái)?shù)據(jù)源和代理協(xié)議
@interface YYViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
/**
 *  水果
 */
@property (strong, nonatomic) IBOutlet UILabel *fruitLab;
/**
 *  主菜
 */
@property (strong, nonatomic) IBOutlet UILabel *stapleLab;
/**
 *  飲料
 */
@property (strong, nonatomic) IBOutlet UILabel *drinkLab;
/**
 *  保存所有的數(shù)據(jù)
 */
@property(nonatomic,strong)NSArray *foods;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
- (IBAction)randomFood:(id)sender;

@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //在這里設(shè)置下方數(shù)據(jù)刷新部分的初始顯示
    for (int component = 0; component<self.foods.count; component++) {
        [self pickerView:nil didSelectRow:0 inComponent:component];
    }
}

#pragma mark-使用懶加載,把數(shù)據(jù)信息加載進(jìn)來
-(NSArray *)foods
{
    if (_foods==nil) {
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
        NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
        _foods=arrayM;
    }
    return _foods;
}

#pragma mark-處理隨機(jī)按鈕的點(diǎn)擊事件
- (IBAction)randomFood:(id)sender {
   
    // 讓pickerView主動(dòng)選中某一行
    // 讓pickerView選中inComponent列的Row行
    //    [self.pickerView selectRow:1 inComponent:0 animated:YES];
   
    /*
     [self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES];
     [self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES];
     [self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES];
     */
   
    //    [self.foods objectAtIndex:0]; == self.foods[0];
    //    [self.foods[0] count];
   
    /*
     // 根據(jù)每一列的元素個(gè)數(shù)生成隨機(jī)值
     [self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES];
     [self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES];
     [self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES];
     */
   
    //設(shè)置一個(gè)隨機(jī)數(shù)
    for (int component=0; component<self.foods.count; component++) {
        //獲取當(dāng)前列對(duì)應(yīng)的數(shù)據(jù)元素的個(gè)數(shù)
        int total=[self.foods[component] count];
        //根據(jù)每一列的總數(shù)生成隨機(jī)數(shù)(當(dāng)前生成的隨機(jī)數(shù))
        int randomNumber=arc4random()%total;
       
        //獲取當(dāng)前選中的行(上一次隨機(jī)后移動(dòng)到的行)
        int oldRow=[self.pickerView selectedRowInComponent:0];
       
        //比較上一次的行號(hào)和當(dāng)前生成的隨機(jī)數(shù)是否相同,如果相同的話則重新生成
        while (oldRow==randomNumber) {
                randomNumber=arc4random()%total;
        }
       
        //讓pickerview滾動(dòng)到指定的某一行
        [self.pickerView selectRow:randomNumber inComponent:component animated:YES];
        //模擬,通過代碼選中某一行
        [self pickerView:nil didSelectRow:randomNumber inComponent:component];
    }
}

#pragma mark- 設(shè)置數(shù)據(jù)
//一共多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}

//每列對(duì)應(yīng)多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //1.獲取當(dāng)前的列
    NSArray *arayM= self.foods[component];
    //2.返回當(dāng)前列對(duì)應(yīng)的行數(shù)
    return arayM.count;
}

//每列每行對(duì)應(yīng)顯示的數(shù)據(jù)是什么
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //1.獲取當(dāng)前的列
    NSArray *arayM= self.foods[component];
    //2.獲取當(dāng)前列對(duì)應(yīng)的行的數(shù)據(jù)
    NSString *name=arayM[row];
    return name;
}

#pragma mark-設(shè)置下方的數(shù)據(jù)刷新
// 當(dāng)選中了pickerView的某一行的時(shí)候調(diào)用
// 會(huì)將選中的列號(hào)和行號(hào)作為參數(shù)傳入
// 只有通過手指選中某一行的時(shí)候才會(huì)調(diào)用
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //獲取對(duì)應(yīng)列,對(duì)應(yīng)行的數(shù)據(jù)
    NSString *name=self.foods[component][row];
    //賦值
    if (0==component) {
        self.fruitLab.text=name;
    }else if(1==component)
    {
        self.stapleLab.text=name;
    }else
        self.drinkLab.text=name;
}

#pragma mark-隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


四、重要補(bǔ)充

請(qǐng)注意在代碼實(shí)現(xiàn)中為什么使用 [self.foods[0] count]; 而不是直接使用點(diǎn)語法self.foods[0].count取值。    

[self.foods objectAtIndex:0]; == self.foods[0];//這兩句的效果等價(jià),而self調(diào)用objectAtIndex:0這個(gè)方法,返回的是一個(gè)id類型的萬能指針,它的真實(shí)類型要到實(shí)際運(yùn)行的時(shí)候才能檢測(cè)得到,因此不能直接使用self.foods[0].count。

相關(guān)文章

最新評(píng)論