2013-12-11 52 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