基于Condition在第二个tableview中添加和删除Row

时间:2013-12-11 07:17:44

标签: ios uitableview uiviewcontroller

我在一个UITableViews中有2 ViewController表示table1和table2。单击一个表中的项目1我将该项目添加到table2。表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];
}

谢谢。

1 个答案:

答案 0 :(得分:1)

出于您的目的,您必须在variables/property中创建一些Viewcontrller1。这些属性用于决策,您可以将项目移至table2

Viewcontrller1中创建两个属性,如

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

Viewcontrller2中创建属性,如

@property(nonatomic,retain) UIViewController *firstViewController;

当您点击table1的任何单元格时,将indexPath.row中的indexRow存储为

self.indexRow = indexPath.row

并通过将Viewcontrller1引用存储到已创建的Viewcontrller2属性,从Viewcontrller1移至firstViewController

Viewcontrller2.firstViewController = self;

现在在Viewcontrller2按下保存按钮然后更改bool值YES / NO(初始化时正好相反),如下所示

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

希望这可以帮助你...