简体   繁体   中英

how can i make uipickerview with multiple row selection and displaying liste options selected in uitextfield iphone?

i have to make a picker view with multiple row selection. i have tried this solution but i was failed for me.

-(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];
    }
}

对于pickerView中的多行选择,您可以使用https://github.com/alexleutgoeb/ALPickerView

for mutliple selection we have to create custom picker by using Tableview. once see this one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM