如何监听pickerview 在滚动

 我来答
千锋教育
2016-08-28 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
pickerview 滚动方法
// SensorAddDeleteViewController.m
// Abviewer_iPhone
//
// Created by Ken.zhao on 13-11-20.
// Copyright (c) 2013年 sven. All rights reserved.
//

#import "SensorAddDeleteViewController.h"
#import "ABServices.h"
#import "FormatWeekListViewController.h"

@interface SensorAddDeleteViewController ()

@end

@implementation SensorAddDeleteViewController
@synthesize m_pickerSensor;
@synthesize m_mutArrSensorList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:K_contentsOfFile(@"Background", @"png")];

NSMutableArray *mutArrSensorList = [[NSMutableArray alloc] initWithArray:[FormatWeekListViewController resultAllSersonName]];
self.m_mutArrSensorList = mutArrSensorList;
[mutArrSensorList release];

UIPickerView *pickerSensor = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 220)];
self.m_pickerSensor = pickerSensor;
[pickerSensor release];
m_pickerSensor.delegate = self;
m_pickerSensor.dataSource = self;
m_pickerSensor.showsSelectionIndicator = YES;
[self.view addSubview:m_pickerSensor];

  //这里是初始化,自动转一圈,避免第一次是数组第一个值造成留白
[m_pickerSensor selectRow:[m_mutArrSensorList count] inComponent:0 animated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)dealloc
{
[m_pickerSensor release];
[m_mutArrSensorList release];
[super dealloc];
}
#pragma mark -
#pragma mark pickview delegate
//组件数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

//每个组件的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [m_mutArrSensorList count]*50;
}

//初始化每个组件每一行数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [m_mutArrSensorList objectAtIndex:(row%[m_mutArrSensorList count])];
}

//选中picker cell,save ArrayIndex
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUInteger max = 0;
NSUInteger base10 = 0;
if(component == 0)
{
max = [m_mutArrSensorList count]*50;
base10 = (max/2)-(max/2)%[m_mutArrSensorList count];
[pickerView selectRow:[pickerView selectedRowInComponent:component]%[m_mutArrSensorList count]+base10 inComponent:component animated:false];
}
}

//替换text居中
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 0.0f, [pickerView rowSizeForComponent:component].width-12, [pickerView rowSizeForComponent:component].height)];

label.text = [m_mutArrSensorList objectAtIndex:(row%[m_mutArrSensorList count])];//[m_mutArrSensorList objectAtIndex:row-1];
label.textAlignment = UITextAlignmentCenter;
return [label autorelease];
}
@end
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式