gpt4 book ai didi

ios - UIPickerView 中的多个选定选项,其中一个部分

转载 作者:行者123 更新时间:2023-11-30 14:02:02 25 4
gpt4 key购买 nike

UIPickerView 中是否可以有多个选定选项?
例如, var array = [1,2,3,4,5] 在 UIPickerView 中同时选择 [1,3,5] 仅一个UIPickerView 中的部分。
提前致谢。

最佳答案

这里很好control这完全是一样的。

还有一种方法可以解决仅使用 UITapGestureRecognizer 的问题。

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

swift

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com