我有一份个人资料表格,作为 iOS 应用程序注册过程的一部分。
我想为性别、职位、出生日期等项目使用“下拉”菜单。每个项目的数据都是静态的——我将使用 UIPickerView 来实现——但我的问题是——我需要吗创建数组和数据委托(delegate)来填充每个单独的选择器,或者是否有更简单的方法来应用静态数据?
最佳答案
你能在没有代表的情况下做到吗?没有。
你可以不用数组吗?是的,但你不应该。
这是一个没有数组的例子(来自 http://cocoamatic.blogspot.com/2010/08/create-uipickerview-programmatically.html )。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = [@"" stringByAppendingFormat:@"Row %d",row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
但是,您真的应该只使用数组,这样可以更轻松地阅读和维护。如果你想添加一个额外的值,你只需将它添加到你的数组中。无需在多个位置进行更新,只需向数组添加另一个值即可。而且,更容易理解。
@implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray = @[@"Row 1", @"Row 2", @"Row 3",];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return _myArray.count;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = myArray[row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
如果您有多个选择器,您只需使用多个数组并检查每个 PickerView 以查看您拥有哪个,因为 PickerView 已传递到列出的每个函数中。
@implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray1 = @[@"Row 1", @"Row 2", @"Row 3",];
_myArray2 = @[@"Row 1-2", @"Row 2-2", @"Row 3-2", @"Row 4-2"];
UIPickerView pickerView1 = [[UIPickerView alloc] init];
UIPickerView pickerView2 = [[UIPickerView alloc] init];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
if (pickerView == pickerView1)
{
// First Picker
}
else if (pickerView == pickerView2)
{
// First Picker
}
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return _myArray1.count;
}
else if (pickerView == pickerView2)
{
// Second Picker
return _myArray2.count;
}
// A third picker passed in somehow
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (pickerView == pickerView1)
{
// First Picker
return 1;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 1;
}
// A third picker passed in somehow
return 0;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if (pickerView == pickerView1)
{
// First Picker
title = myArray1[row];
}
else if (pickerView == pickerView2)
{
// Second Picker
rtitle = myArray2[row];
}
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return 300;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 400;
}
// A third picker passed in somehow
return 0;
}
关于iOS - 填充静态 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191718/
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我正在尝试解决http://projecteuler.net/problem=1.我想创建一个方法,它接受一个整数,然后创建一个包含它前面的所有整数的数组,并将整数本身作为数组中的值。以下是我目前所拥有的。代码不起作用。defmake_array(num)numbers=Array.newnumcount=1numbers.eachdo|number|numbers 最佳答案 (1..num).to_a是您在Ruby中需要做的全部。1..num将创建一个Range对象,以1开始并以任意值num结束是。Range对象有to_a方法通过
如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one
我写了一个脚本,其中包含一些方法定义,没有类和一些公共(public)代码。其中一些方法执行一些非常耗时的shell程序。然而,这些shell程序只需要在第一次调用该方法时执行。现在在C中,我会在每个方法中声明一个静态变量,以确保这些程序只执行一次。我怎么能在Ruby中做到这一点? 最佳答案 ruby中有一个成语:x||=y。defsomething@something||=calculate_somethingendprivatedefcalculate_something#somelongprocessend但是如果您的“长时间
伙计们,我正在学习ruby,最近从JAVA转行。在JAVA中,我可以将类的成员变量设为静态,并且该成员变量在类的实例中保持不变。我如何在ruby中实现相同的目标。我在我的ruby课上做了这样的事情:classBaseclass@@wordshashend到目前为止,这似乎在我测试时达到了目的,即@@wordhash在Baseclass的实例中保持不变。我的理解对吗?另外,我想在类中有一个成员方法,相当于JAVA中的静态方法(我不需要类的实例来访问它)。我怎样才能做到这一点?例如,我想在Baseclass中有一个像getwordshash()这样的方法,它返回@@wordshas