ios - 如何使用数据快速优化代码和加载 UIPickerView?
<p><p>我正在创建包含 2 个组件的 UIPickerView,时间分别为几分钟和几秒钟。我已经在 UI 中创建了选择器并希望用数据填充它,下面是我如何用 0 到 59 的数字填充它的代码。我想让它看起来像圆形,这就是 kSizeOfPicker 为 60000 的原因。当用户按下按钮窗口时选择器出现,但它非常减慢应用程序,因为这
代码在 viewDidLoad 中。我该如何解决?</p>
<pre><code>NSString *stringValue = [ init];
for(int i=0; i<kSizeOfPicker; i++)
{
stringValue = ;
;
;
}
</code></pre>
<p>这里是数据源和委托(delegate)方法:</p>
<pre><code>#pragma mark - UIPickerView DataSource Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent : (NSInteger)component
{
if (component==0)
{
return ;
}
else
{
return ;
}
</code></pre>
<p>}</p>
<pre><code>- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
switch (component)
{
case 0:
title = ];
return title;
break;
case 1:
title = ];
return title;
break;
}
return nil;
}
#pragma mark - UIPickerView Delegate Methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == 0)
_firstComponent = ;
else
_secondComponent = ;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您不需要准备 NSArray 中的所有数据来归档循环选取器 View 。
以下代码<strong>实际上</strong>在选定行周围准备了 +-50 个重复数据集。
在 <code>pickerView:didSelectRow:inComponent:</code> 委托(delegate)方法中,您可以重置选定的行。 </p>
<pre><code>@interface MyViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (assign, nonatomic) NSUInteger minutes;
@property (assign, nonatomic) NSUInteger seconds;
@end
@implementation MyViewController
#define VALUE_THRESHOLD 60
#define PICKER_DUPLICATES 100
#define CENTER_ROW (VALUE_THRESHOLD * PICKER_DUPLICATES / 2)
- (void)viewDidLoad {
;
;
;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return VALUE_THRESHOLD * PICKER_DUPLICATES;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return ;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSInteger actualRow = row % VALUE_THRESHOLD;
// reset to center dataset
;
// do anything what you want;
if(component == 0) {
_minutes = actualRow;
}
else {
_seconds = actualRow;
}
}
@end
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用数据快速优化代码和加载 UIPickerView?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24795700/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24795700/
</a>
</p>
页:
[1]