如何 select 来自 UIPickerView 的多行 ios
How to select multiple rows from UIPickerView ios
我想 select 从 UIPickerView 中多出一行并放在 UITextField 中。我项目中使用的语言是 objective c 。我不懂逻辑
您可以创建一个新的表格视图,将每一行作为一种语言,然后在用户响应文本字段时显示它。
您可以查看 here 一些关于如何实现多选逻辑的起始代码。
文本字段有一个 属性 调用 inputview
,您可以尝试用 tableview 替换它。
据我所知,您想要实现的是您想要 select 列表中的多个项目并将其显示在 UITextField
中。为此,您可以这样做:
首先将 UITableView
拖放到 storyboard
中并连接 datasource
和 delegates
。
将 UITableViewCell
拖入 tableview
并将 reuserIdentifier
作为 TableCell.
将 UILabel
和 UIImageView
拖到 tableCell
上。将 label
和 imageView
标记为 888 和 999。把 imageview
做成一个小盒子,放在你的 label
.
旁边
使用 array
来显示您想要向用户显示 select 项的列表,并用它填充 UITableView
。
拿另一个 array
来存储 selected 项目。
就在你的实现下面声明两个这样的数组:
@implementation ViewController {
NSMutableArray *arrItems;
NSMutableArray *arrSelectedItems;
}
现在在您的 viewDidLoad
中填充您的项目 array
。
- (void)viewDidLoad {
[super viewDidLoad];
arrItems = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSString stringWithFormat:@"Item%d",i+1] forKey:@"itemName"];
[dict setObject:@"0" forKey:@"isSelected"];
[arrItems addObject:dict]
}
arrSelectedItems = [[NSMutableArray alloc]init];
}
现在实现 UITableView
的 DataSource
和 Delegate
方法。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrItems.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
NSMutableDictionary *dict = [arrItems objectAtIndex:indexPath.row];
UILabel *lblItemName = (UILabel *)[cell.contentView viewWithTag:888];
lblItemName.text = [dict valueForKey:@"itemName"];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:999];
if ([[dict valueForKey:@"isSelected"]isEqualToString:@"0"]) {
imageView.backgroundColor = [UIColor redColor];
} else {
imageView.backgroundColor = [UIColor greenColor];
}
return cell;
}
现在在 didSelectRowAtIndexPath
中实现多个 selection 登录,如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *dict = [arr objectAtIndex:indexPath.row];
if ([arrSelectedItems containsObject:dict]) {
[arrSelectedItems removeObject:dict];
} else {
[arrSelectedItems addObject:dict];
}
if ([[dict valueForKey:@"isSelected"]isEqualToString:@"0"]) {
[dict setObject:@"1" forKey:@"isSelected"];
} else {
[dict setObject:@"0" forKey:@"isSelected"];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//Here you can show the selected Objects in `UITextField`
self.yourTexField.text = [arrSelectedItems componentsJoinedByString:@","];
}
P.S: I have written the total code here, haven't tested it, so if any errors occurs feel free to leave a comment below, I will be there to solve it.
我想它会对你有所帮助。
我想 select 从 UIPickerView 中多出一行并放在 UITextField 中。我项目中使用的语言是 objective c 。我不懂逻辑
您可以创建一个新的表格视图,将每一行作为一种语言,然后在用户响应文本字段时显示它。
您可以查看 here 一些关于如何实现多选逻辑的起始代码。
文本字段有一个 属性 调用 inputview
,您可以尝试用 tableview 替换它。
据我所知,您想要实现的是您想要 select 列表中的多个项目并将其显示在 UITextField
中。为此,您可以这样做:
首先将 UITableView
拖放到 storyboard
中并连接 datasource
和 delegates
。
将 UITableViewCell
拖入 tableview
并将 reuserIdentifier
作为 TableCell.
将 UILabel
和 UIImageView
拖到 tableCell
上。将 label
和 imageView
标记为 888 和 999。把 imageview
做成一个小盒子,放在你的 label
.
使用 array
来显示您想要向用户显示 select 项的列表,并用它填充 UITableView
。
拿另一个 array
来存储 selected 项目。
就在你的实现下面声明两个这样的数组:
@implementation ViewController {
NSMutableArray *arrItems;
NSMutableArray *arrSelectedItems;
}
现在在您的 viewDidLoad
中填充您的项目 array
。
- (void)viewDidLoad {
[super viewDidLoad];
arrItems = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSString stringWithFormat:@"Item%d",i+1] forKey:@"itemName"];
[dict setObject:@"0" forKey:@"isSelected"];
[arrItems addObject:dict]
}
arrSelectedItems = [[NSMutableArray alloc]init];
}
现在实现 UITableView
的 DataSource
和 Delegate
方法。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return arrItems.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
NSMutableDictionary *dict = [arrItems objectAtIndex:indexPath.row];
UILabel *lblItemName = (UILabel *)[cell.contentView viewWithTag:888];
lblItemName.text = [dict valueForKey:@"itemName"];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:999];
if ([[dict valueForKey:@"isSelected"]isEqualToString:@"0"]) {
imageView.backgroundColor = [UIColor redColor];
} else {
imageView.backgroundColor = [UIColor greenColor];
}
return cell;
}
现在在 didSelectRowAtIndexPath
中实现多个 selection 登录,如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *dict = [arr objectAtIndex:indexPath.row];
if ([arrSelectedItems containsObject:dict]) {
[arrSelectedItems removeObject:dict];
} else {
[arrSelectedItems addObject:dict];
}
if ([[dict valueForKey:@"isSelected"]isEqualToString:@"0"]) {
[dict setObject:@"1" forKey:@"isSelected"];
} else {
[dict setObject:@"0" forKey:@"isSelected"];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//Here you can show the selected Objects in `UITextField`
self.yourTexField.text = [arrSelectedItems componentsJoinedByString:@","];
}
P.S: I have written the total code here, haven't tested it, so if any errors occurs feel free to leave a comment below, I will be there to solve it.
我想它会对你有所帮助。