我是 iOS 新手,在向文本字段添加多选表值时遇到问题。 我的代码是这样的 在 DidSelectRowAtIndexPath 中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
txtactivity.text=[activityarray objectAtIndex:indexPath.row];
lblactivity.text=[NSString stringWithFormat:@"%@",[idActivityarray objectAtIndex:indexPath.row]];
txtactivity.text=[NSString stringWithFormat:@"%@",[activityarray objectAtIndex:indexPath.row]];
[[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
我得到这样的输出
CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSString stringWithFormat:@"%@",[activityarray objectAtIndex:indexPath.row]];
return cell;
}
我的问题是我在文本框中只得到一个值。我需要在文本框中获取所有选定的值。这个怎么做?提前致谢!
最佳答案
获取一个 NSMutableArray 并在 viewDidLoad
初始化它。然后使用下面的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!arrSelectedValue containsObject:[activityarray objectAtIndex:indexPath.row]){
[arrSelectedValue addObject:[activityarray objectAtIndex:indexPath.row]];
txtactivity.text = [arrSelectedValue componentsJoinedByString:@","];
}
[[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
- (void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(arrSelectedValue containsObject:[activityarray objectAtIndex:indexPath.row]){
[arrSelectedValue removeObject:[activityarray objectAtIndex:indexPath.row]];
txtactivity.text = [arrSelectedValue componentsJoinedByString:@","];
}
[[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
关于ios - 如何将多选 UITableView 值添加到 objective-c 中的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41930272/