如何将多个选定的 uitableviewcells 添加到同一 uitableviewcontroller 中的空 uitableview 单元格

how to Add multiple selected uitableviewcells to an empty uitableview cell in a same uitableviewcontroller

您好,我在 UIViewController 中有 2 个 UITableviews(tableview1 和 tableview2)。我已经使用 MVC 方法将一些数组加载到 tableview1,但将 tableview2 保持为空。无论如何,我需要根据下面的 UI 点击添加按钮将选定的行从 tableview1 加载到 tableview2,或者使用 UILongpressgesture.

将 tableview1 选定的单元格拖到 tableview2

任何人都可以给我相关 iOS (objective-c) 教程、说明或示例项目存储库的链接以获取一些想法。

这就是我将数据加载到 TableView1 的方式

- (void)viewDidLoad {
    [super viewDidLoad];

    tableView1.dataSource =self;
    tableView2.delegate=self;
    tableView2.dataSource=self;
    tableView2.delegate=self;
    self.cellSelected=[NSMutableArray array];




    //Personal Assigned Array
    SAassigned *psaUser1=[SAassigned new];
    psaUser1.psaName=@"Peter Parker";
    psaUser1.psaimage=@"men1.jpg";

    SAassigned *psaUser2=[SAassigned new];
    psaUser2.psaName=@"Michel Jordan";
    psaUser2.psaimage=@"men2.jpg";

    SAassigned *psaUser3=[SAassigned new];
    psaUser3.psaName=@"Tiego Costa";
    psaUser3.psaimage=@"men3.jpg";

    SAassigned *psaUser4=[SAassigned new];
    psaUser4.psaName=@"Ketty perry";
    psaUser4.psaimage=@"women1.jpg";

    SAassigned *psaUser5=[SAassigned new];
    psaUser5.psaName=@"Jimmy More";
    psaUser5.psaimage=@"men1.jpg";

    SAassigned *psaUser6=[SAassigned new];
    psaUser6.psaName=@"James Franklin";
    psaUser6.psaimage=@"men2.jpg";

    SAassigned *psaUser7=[SAassigned new];
    psaUser7.psaName=@"Peter Parker";
    psaUser7.psaimage=@"men3.jpg";

    SAassigned *psaUser8=[SAassigned new];
    psaUser8.psaName=@"Jessica Helan";
    psaUser8.psaimage=@"women1.jpg";

    assignedUsers=[NSMutableArray arrayWithObjects:psaUser1,psaUser2,psaUser3,psaUser4,psaUser5,psaUser6,psaUser7,psaUser8, nil];

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



        //tableView 1
        PACell *cell=[tableView dequeueReusableCellWithIdentifier:@"pCell"];

        if([self.cellSelected containsObject:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else
        {
            cell.accessoryType=UITableViewCellAccessoryNone;
        }

        if (cell==nil) {

            cell=[[PACell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pCell"];
        }

        //tableview 2
        PSCell *cell2=[tableView dequeueReusableCellWithIdentifier:@"pCell2"];
        if([self.cellSelected containsObject:indexPath])
        {
            cell2.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else
        {
            cell2.accessoryType=UITableViewCellAccessoryNone;
        }

        if (cell2==nil) {

            cell2=[[PSCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pCell2"];
        }

        SAassigned *assigned=nil;

        if(tableView == self.tableView1)
        {
            assigned=[assignedUsers objectAtIndex:indexPath.row];
            cell.persName.text=assigned.psaName;
            cell.persImage.image=[UIImage imageNamed:assigned.psaimage];
        }
        else if(tableView == self.tableView2)
        {
            // not yet implemented / loaded - empty
            return cell2;
        }
        return cell;
}

每次用户 clicks/selects 单元格时,将选定的行数据添加到单独的数组中。并在将所选数据显示到另一个单元格中的同时,使用该数组加载它们。

您需要创建实例数组对象。

NSMutableArray *selectedArray = [NSMutableArray alloc]init];

然后您需要从 "selectedArray" 添加和删除对象。

  if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectedArray removeAtIndex:indexPath.row]
   } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectedArray insertObject:yourObject atIndex:indexPath];.row]
    }