ios - 如何将多选 UITableView 值添加到 objective-c 中的 UITextField

标签 ios objective-c uitableview

我是 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];
}

我得到这样的输出

enter image description here

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/

相关文章:

ios - 自动布局旋转固定位置

objective-c - 调用从 PyObjC 获取 char* 的选择器

ios - 将 UIButton 锚定到 UITableViewController View 的底部

ios - 用文本填充单元格中的标签

ios - UITapGestureRecognizer 与 didSelectRowAtIndexPath 并发性

ios - UIWeb View iOS 5 : WebKit/JavaScriptCore crash

ios - 搜索DisplayController 到detailedViewController

ios - 如何为我们的应用程序使用可访问性不同的字体大小?

ios - dispatch_source_t 处理函数的计时问题——我是否遵循正确的调度计时器模式?

ios - 我需要在下一次分配之前释放吗?