UIPickerView中是否可以有几个选择的选项?
例如,在UIPickerView中的var array = [1,2,3,4,5]只能在UIPickerView中只有一个部分的情况下选择[1,3,5]
提前致谢。

最佳答案

这是很好的control,它做的完全一样。

也可以使用UITapGestureRecognizer解决。

目标C

- (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)];
        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleSelection:)];
        singleTapGestureRecognizer.numberOfTapsRequired = 1;
        [cell addGestureRecognizer:singleTapGestureRecognizer];
    }

    if ([selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    cell.textLabel.text = [datasource objectAtIndex:row];
    cell.tag = row;

    return cell;
}

- (void)toggleSelection:(UITapGestureRecognizer *)recognizer {
    NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag];
    NSUInteger index = [selectedItems indexOfObject:row];
    if (index != NSNotFound) {
        [selectedItems removeObjectAtIndex:index];
        [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone];
    } else {
        [selectedItems addObject:row];
        [(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
}


迅速

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView) -> UIView {
    var cell: UITableViewCell = view
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyleDefault, reuseIdentifier: nil)
        cell.setBackgroundColor(UIColor.clearColor())
        cell.setBounds(CGRectMake(0, 0, cell.frame.size.width - 20, 44))
        var singleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "toggleSelection:")
        singleTapGestureRecognizer.numberOfTapsRequired = 1
        cell.addGestureRecognizer(singleTapGestureRecognizer)
    }
    if selectedItems.indexOfObject(NSNumber.numberWithInt(row)) != NSNotFound {
        cell.setAccessoryType(UITableViewCellAccessoryCheckmark)
    }
    else {
        cell.setAccessoryType(UITableViewCellAccessoryNone)
    }
    cell.textLabel.text = datasource.objectAtIndex(row)
    cell.tag = row
    return cell
}

func toggleSelection(recognizer: UITapGestureRecognizer) {
    var row: Int = NSNumber.numberWithInt(recognizer.view.tag)
    var index: UInt = selectedItems.indexOfObject(row)
    if index != NSNotFound {
        selectedItems.removeObjectAtIndex(index)
        (recognizer.view).setAccessoryType(UITableViewCellAccessoryNone)
    }
    else {
        selectedItems.addObject(row)
        (recognizer.view).setAccessoryType(UITableViewCellAccessoryCheckmark)
    }
}

关于ios - UIPickerView中的几个选定选项,只有一个部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32878599/

10-10 06:33