Ios 如何替换NSMutableArray列表数据
我对IOS非常陌生。在我的应用程序中,我在我的Ios 如何替换NSMutableArray列表数据,ios,objective-c,nsmutablearray,Ios,Objective C,Nsmutablearray,我对IOS非常陌生。在我的应用程序中,我在我的mainViewController上插入了两个ui按钮 当我单击第一个按钮时,我正在我的主NSMutableArray中添加一些数据,当我单击“第二个”按钮时,我希望删除以前的数组详细信息并替换新数据 我的代码: 请帮助。尝试此代码 - (IBAction)button1:(id)sender{ NSMutableArray * data1 = [[NSMutableArray alloc] initWithObjects:@"1
mainViewController上插入了两个ui按钮
当我单击第一个按钮时,我正在我的主NSMutableArray
中添加一些数据,当我单击“第二个”按钮时,我希望删除以前的数组详细信息并替换新数据
我的代码:
请帮助。尝试此代码
- (IBAction)button1:(id)sender{
NSMutableArray * data1 = [[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3", nil];
[mainArray addObject:data1];
}
- (IBAction)button2:(id)sender{
NSMutableArray * data2 = [[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
[mainArray replaceObjectAtIndex:0 withObject:data2];
}
我假设main数组只包含一个元素。如果有更多元素需要指定正确的索引,请尝试以下方法:
- (IBAction)button1:(id)sender{
[mainArray addObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3", nil]];
}
- (IBAction)button2:(id)sender{
[mainArray replaceObjectsInRange:NSMakeRange(0, mainArray.count) withObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:@"6",@"2",@"3",@"4",@"5", nil]];
}
- (IBAction)button1:(id)sender{
[mainArray addObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3", nil]];
}
- (IBAction)button2:(id)sender{
[mainArray replaceObjectsInRange:NSMakeRange(0, mainArray.count) withObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:@"6",@"2",@"3",@"4",@"5", nil]];
}
- (void)viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc]init];
}
- (IBAction)button1:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3", nil];
}
- (IBAction)button2:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
}