gpt4 book ai didi

ios - 根据条件在第二个 TableView 中添加和删除行

转载 作者:行者123 更新时间:2023-11-29 03:25:26 25 4
gpt4 key购买 nike

我在一个 ViewController 中有 2 个 UITableViews,例如 table1 和 table2。单击表 1 中的某个项目后,我将该项目添加到表 2 中。 table1 中的其中一项表示 objectAtIndex 1 显示另一个 Viewcontrller。第二个 View Controller 有一个后退按钮和保存按钮。仅当单击第二个 View Controller 中的“保存”按钮时,我才想将此项目添加到表 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];
}

谢谢。

最佳答案

为了达到您的目的,您必须在 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

并通过将 Viewcontller1 引用存储到创建的 firstViewController 属性,从 Viewcontller1 移动到 Viewcontller2

>
Viewcontrller2.firstViewController = self;

现在在Viewcontrller2中,当您按下保存按钮时,然后更改 bool 值YES/NO(与初始化时相反)如下

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

现在,您在 Viewcontrller1 - (void)viewWillAppear:(BOOL)animated{} 方法中编写逻辑,将项目从一个数组移动到另一个数组,并在重新加载后你的两个桌面 View 。

- (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 - 根据条件在第二个 TableView 中添加和删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com