你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發中運用Picker View完成一個點菜使用的UI示例

iOS開發中運用Picker View完成一個點菜使用的UI示例

編輯:IOS開發綜合

一、完成效果

  闡明:點擊隨機按鈕,可以自動選取,下方數據自動刷新。

二、完成思緒

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文件

storyboard文件大的界面設置:

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)prefeXmlRss/ target=_blank class=infotextkey>XmlRss/ target=_blank class=infotextkey>RsstatusBarHidden
{
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開發中運用Picker View完成一個點菜使用的UI示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved