2013-05-02 72 views
1

我必須做出多行選擇的選擇器視圖。我已經嘗試過這個解決方案,但我失敗了。我如何使uipickerview與多行選擇和顯示在uitextfield iphone中選擇的選項?

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


     UITableViewCell *cell = (UITableViewCell *)view; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:nil]; 
     [cell setBackgroundColor:[UIColor clearColor]]; 
     [cell setBounds:CGRectMake(0, 0, cell.frame.size.width - 20, 44)]; 
     cell.tag = row ; 
     UITapGestureRecognizer * singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] 
                   initWithTarget:self 
                   action:@selector(toggleSelection:)]; 
     singleTapGestureRecognizer.numberOfTapsRequired = 1; 
     [cell addGestureRecognizer:singleTapGestureRecognizer]; 
    } 
    if ([self.selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) { 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
    cell.textLabel.text = [self.options objectAtIndex:row]; 

    return cell; 

} 

-(void)toggleSelection:(UITapGestureRecognizer *)recognizer { 

    NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag]; 
    NSUInteger index = [self.selectedItems indexOfObject:row]; 

    if (index != NSNotFound) { 

     [self.selectedItems removeObjectAtIndex:index]; 
     [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone]; 
    } else { 

     [self.selectedItems addObject:row]; 
     [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } 
} 
+0

你在viewForRow方法中做了什麼? – Balu 2013-05-02 10:03:00

回答