我在一个UITableViews中有2个ViewController,例如table1和table2。在一个表1中单击一个项目,我将该项目添加到表2中。表1中的其中一项表示objectAtIndex 1显示了另一个Viewcontrller。第二个viewcontroller具有后退按钮和保存按钮。我只想单击第二个视图控制器中的“保存”按钮,才将此项目添加到表2中。有想法吗?

- (void)viewWillAppear:(BOOL)animated
{
if (self.moveItem && self.indexRow != -1) {
    // Your logic to move data from one array to another based on self.indexRow
    // May like below
    // item = [firtsArray objectAtIndex:self.indexRow];
    // [secondArray arrayByAddingObject:item];
    // [firtsArray removeObjectAtIndex:self.indexRow];
    // Reload your both table view

    [selectedCallType addObject:[callType objectAtIndex:self.indexRow]];
    [selectedCallTypeId addObject:[callTypeId objectAtIndex:self.indexRow]];

    self.moveItem = NO;
}

NSLog(@"Call Type Array = %@",selectedCallType);
NSLog(@"Call Type Id Array = %@",selectedCallTypeId);

[table reloadData];
[table2 reloadData];
}


谢谢。

最佳答案

为了您的目的,您必须在variables/property中创建一些Viewcontrller1。这些属性用于决策中,您准备将其移至table2

Viewcontrller1中创建两个属性,如下所示

@property(nonatomic) NSInteger indexRow;
@property(nonatomic) BOOL moveItem;


Viewcontrller2中创建属性,如下所示

@property(nonatomic,retain) UIViewController *firstViewController;


当您单击表1的任何单元格时,将其indexPath.row存储为indexRow作为

self.indexRow = indexPath.row


并通过存储对创建的Viewcontrller1属性的Viewcontrller2引用从Viewcontrller1移到firstViewController如下

Viewcontrller2.firstViewController = self;


现在,在Viewcontrller2中,当您按保存按钮,然后按以下方式更改布尔值YES / NO(初始化时正好相反)

// (Let us suppose you initialise it with NO)
self.firstViewController.moveItem = YES


现在,您在Viewcontrller1 - (void)viewWillAppear:(BOOL)animated{}方法中编写逻辑,以将项目从一个数组移到另一个数组,然后将其重新加载到两个表视图中。

- (void)viewDidLoad
 {
     [super viewDidLoad];
     self.indexRow = -1;
     self.moveItem = NO;
     // Your other code....
 }

- (void)viewWillAppear:(BOOL)animated{
   if (self.moveItem && self.indexRow != -1) {
       // Your logic to move data from one array to another based on self.indexRow
       // May like below
       // item = [firtsArray objectAtIndex:self.indexRow];
       // [secondArray arrayByAddingObject:item];
       // [firtsArray removeObjectAtIndex:self.indexRow];
       // Reload your both table view

       // Create New reference of adding object then add it to another array.
       // This will create new reference of adding object, Now add it to second array like below
       // Hope this work. See the array count.
       id *addObject = [callType objectAtIndex:self.indexRow];
       id *addObjectID = [callTypeId objectAtIndex:self.indexRow];
       [selectedCallType addObject:addObject];
       [selectedCallTypeId addObject:addObjectID];

       [table reloadData];
       [table2 reloadData];
       self.moveItem = NO;
    }
}


希望这可以帮助您...

关于ios - 根据条件在第二个表 View 中添加和删除行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20512848/

10-12 01:57