1

我尝试将我的选择器视图自定义为可检查的,就像您在图片(youtube 网站)中单击 webview 上的下拉列表时一样。

在此处输入图像描述 http://img830.imageshack.us/img830/3747/screenshot20101004at606.png

我使用 viewForRow 方法为选择器中的每一行自定义视图。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

 UIView *rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)] autorelease];
 rowView.backgroundColor = [UIColor clearColor];
 rowView.userInteractionEnabled = NO;

 UIImageView *checkmarkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 24, 19)];

 UIFont *font = [ UIFont boldSystemFontOfSize:18];
 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 240, 44) ];
 NSString *pickerText = [[Itemlist objectAtIndex:row] objectForKey:@"name"];
 titleLabel.text = pickerText;
 titleLabel.textAlignment = UITextAlignmentLeft;
 titleLabel.backgroundColor = [UIColor clearColor];
 titleLabel.font = font;
 titleLabel.opaque = NO;

 if ([selected_property_id intValue] == row) {
  titleLabel.textColor = [UIColor blueColor];
  checkmarkImageView.image = [UIImage imageNamed:@"checkmark.png"];
 }else {
  titleLabel.textColor = [UIColor blackColor];
  checkmarkImageView.image = nil;
 }

 [rowView addSubview:checkmarkImageView];
 [rowView addSubview:titleLabel];
 [titleLabel release];
 [checkmarkImageView release];

 return rowView; 

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 [m_pickerView reloadAllComponents];
}

当我选择我想要的行时,它会完美地为所选行添加复选标记但是当我向上/向下滚动选择器时,选择器将自动选择选择器中间的行。因此,它会在中间行自动添加复选标记。

我的问题是如何禁用中间行的自动选择。当用户在他们想要的行上标签时,它应该添加复选标记(如在 youtube 中)。

我应该对pickerview使用触摸事件方法吗?

谢谢您的帮助。

4

2 回答 2

1

是的,您应该使用触摸事件。但是,标准 UIPickerView 捕获触摸并且不会转发它们,因此视图控制器永远不会接收到事件。

在 Github上创建了一个带有 UIPickerView 子类的项目,该子类允许触摸逃脱它。它将它们转发到其父视图,然后在视图控制器的 touchesEnded 方法中处理选择。它还实现了提到的复选标记。

于 2011-08-13T15:17:18.270 回答
0

Reference this: https://github.com/scelis/SCKit/tree/

It have a example project. really better then code paste here.

于 2012-02-18T12:22:47.663 回答