国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發中使用Picker View實現一個點菜應用的UI示例

2019-10-21 18:56:17
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS開發中使用Picker View實現一個點菜應用的UI示例,代碼基于傳統的Objective-C,需要的朋友可以參考下
 

一、實現效果

  說明:點擊隨機按鈕,能夠自動選取,下方數據自動刷新。

iOS開發中使用Picker View實現一個點菜應用的UI示例

二、實現思路

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

iOS開發中使用Picker View實現一個點菜應用的UI示例

storyboard文件大的界面設置:

iOS開發中使用Picker View實現一個點菜應用的UI示例

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

復制代碼代碼如下:

//
//  YYViewController.m
//  06-簡單選菜系統的實現
//
//  Created by apple on 14-6-5.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYViewController.h"

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

@end


復制代碼代碼如下:

@implementation YYViewController

 

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

#pragma mark-使用懶加載,把數據信息加載進來
-(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-處理隨機按鈕的點擊事件
- (IBAction)randomFood:(id)sender {
    
    // 讓pickerView主動選中某一行
    // 讓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];
    
    /*
     // 根據每一列的元素個數生成隨機值
     [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];
     */
    
    //設置一個隨機數
    for (int component=0; component<self.foods.count; component++) {
        //獲取當前列對應的數據元素的個數
        int total=[self.foods[component] count];
        //根據每一列的總數生成隨機數(當前生成的隨機數)
        int randomNumber=arc4random()%total;
        
        //獲取當前選中的行(上一次隨機后移動到的行)
        int oldRow=[self.pickerView selectedRowInComponent:0];
        
        //比較上一次的行號和當前生成的隨機數是否相同,如果相同的話則重新生成
        while (oldRow==randomNumber) {
                randomNumber=arc4random()%total;
        }
        
        //讓pickerview滾動到指定的某一行
        [self.pickerView selectRow:randomNumber inComponent:component animated:YES];
        //模擬,通過代碼選中某一行
        [self pickerView:nil didSelectRow:randomNumber inComponent:component];
    }
}

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

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

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

#pragma mark-設置下方的數據刷新
// 當選中了pickerView的某一行的時候調用
// 會將選中的列號和行號作為參數傳入
// 只有通過手指選中某一行的時候才會調用
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //獲取對應列,對應行的數據
    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-隱藏狀態欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


四、重要補充

 

請注意在代碼實現中為什么使用 [self.foods[0] count]; 而不是直接使用點語法self.foods[0].count取值。    

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



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舒城县| 克拉玛依市| 西乌| 土默特右旗| 鹰潭市| 政和县| 东平县| 梅州市| 抚顺市| 和硕县| 抚州市| 习水县| 班玛县| 嵊泗县| 西安市| 米泉市| 天柱县| 浑源县| 元氏县| 伊宁县| 吉隆县| 兴仁县| 深圳市| 隆安县| 沅陵县| 太仓市| 噶尔县| 溧水县| 旺苍县| 颍上县| 荃湾区| 滕州市| 昌江| 泸州市| 关岭| 平安县| 二连浩特市| 浦县| 准格尔旗| 明水县| 泰宁县|