【问题标题】:How can I add a row in UIPickerview如何在 UIPickerview 中添加一行
【发布时间】:2013-05-19 21:25:37
【问题描述】:

我有一个 UIPickerView,在 UIPickerView 中添加新记录的绿色按钮。我可以添加一条新记录,但它没有出现,因为我无法更改组件中的行数。

如何在 UIPickerView 中添加记录和行?

谢谢!

我的 UIPickerview:

https://docs.google.com/file/d/0B26lA9znN3CUSTdvX21kUF92MzA/edit?usp=sharing

已编辑:当我单击绿色按钮时,它会显示一个 UIAlertView TextInput,我在其中在 NSMutableArray 中添加了一个新值,因此记录数会发生变化。因此,当 ReloadAllComponents 时,他不会在 UIPickerView 中添加额外的行,因为我已经在 numberOfRowsInComponent 方法中设置了一个数量。

【问题讨论】:

  • 请将您的代码发布在您的选择器视图的数据源和委托上。

标签: ios6 uipickerview


【解决方案1】:

假设您为您的 pickerview 记录管理一个 NSMutableArray。在这种情况下,您必须管理 3 种方法来实现:

1.你的绿色按钮处理方法

-(IBAction) greenbuttonPressed:(id)sender{
    // add new record to your NSMutableArray (mypickerviewrowsarray)

    [self.myPickerView reloadAllComponents];

}

2。 pickeview 的数据源方法 numberOfRowsInComponent

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.mypickerviewrowsarray count];
}

3. pickerview 的委托方法,titleForRow

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.mypickerviewrowsarray objectAtIndex:row];
}

要正确执行此操作,请确保设置您的pickerview 的委托,同时设置您的视图控制器以实现 UIPickerViewDelegate 和 UIPickerViewDataSource。

希望你可以使用它!

【讨论】:

  • 当我点击绿色按钮时,它会显示一个 UIAlertView TextInput 我在我的 NSMutableArray 中添加一个新值,因此记录数会发生变化。因此,当 ReloadAllComponents 时,他不会在 UIPickerView 中添加额外的行,因为我已经在 numberOfRowsInComponent 方法中设置了一个数量。
  • 如果我理解得很好,这意味着应该在 alertview 关闭后重新加载。所以reloadAllComponents 应该是AlertView 的按钮处理程序dismissWithClickedButtonIndex 中的最后一行。你试过这个吗? reloadAllComponents 触发 numberOfRowsInComponent 也应该返回一个递增的值。
  • Really reloadAllComponents 重新加载了 UIPickerView 的所有方法,因此我看到错误不在这些方法中。我在 ViewDidLoad 方法中查询数据库并将这些查询传递给 MSMutableArray。在 numberOfRowsInComponent 方法中,我返回了记录数。我通过在 numberOfRowsInComponent 方法中进行查询解决了这个问题,因此该值将始终被更新。简而言之,我没有再次执行查询,因此数字没有改变。原谅我的英语不好,谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多