2013-12-11 48 views
0

在第二个tableview中添加和删除行我有2 UITableViews在一个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]; 
} 

谢谢。

+0

它只是在一个视图 - 控制两个表。我有2个viewcotrollers – LeXeR

+1

你需要更清楚 – rishi

+0

基本上,当点击表1中的单元格我正在显示另一个有两个按钮(保存和返回)的viewcontroller。只有在第二个视图控制器中单击保存时,我单击的单元格才应添加到表格2中。 – LeXeR

回答

1

为了您的目的,您必须在Viewcontrller1中创建一些variables/property。这些属性决策,你准备好您的物品移到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通过存储Viewcontrller1参照创建firstViewController属性如下

Viewcontrller2.firstViewController = self; 

现在Viewcontrller2当按下保存按钮然后当你初始化它改变它的布尔值YES/NO(只是相对移动到Viewcontrller2 ),如下

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

现在你在Viewcontrller1- (void)viewWillAppear:(BOOL)animated{}方法写你的逻辑,你的项目移动从一个阵列到另一个后它重新加载你的两个tableview。

- (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; 
    } 
} 

希望这可以帮助你......

+0

嘿,thanx for解释。我没有逻辑,我会试试这个。希望是作品 – LeXeR

+1

呀,这是一个可以帮助你的原始想法。如果你仍然有任何问题后,我会尽力帮助你.. –

+0

嗨,我试过你的方法,我认为它工作正常。仍然有一个小错误。点击保存后,我回到firstViewController。第二张桌子似乎重新加载。数据存在于数组中。 – LeXeR