当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS开发中使用Picker View实现一个点菜应用的UI示例

iOS开发中使用Picker View实现一个点菜应用的UI示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

一、实现效果

  说明:点击随机按钮,能够自动选取,下方数据自动刷新。

201611691958282.png (320×502)

二、实现思路

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

201611692023598.png (853×306)

storyboard文件大的界面设置:

201611692040383.png (451×580)

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。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网