gpt4 book ai didi

ios - 如何在 Objective c 的 UITableview 中获取 NSMutableArray 的实际值而不是索引

转载 作者:行者123 更新时间:2023-11-28 18:52:03 24 4
gpt4 key购买 nike

我是 iOS 的新手,我遇到了关于在搜索后获取数组 ID 的问题。我需要在搜索后获取数组的实际值,但是当我搜索数组的内容时,它会给我当前在 tableview 中的数组索引。我的代码是这样的:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil]; //Hear arrofColor is NSMutableArray.
idarray2 =[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; //Hear idarray is NSMutableArray.

[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFilter)
{
return [searchArray count];
}
else
return [arrOfColor count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if(isFilter)
{
cell.textLabel.text=[searchArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text=[arrOfColor objectAtIndex:indexPath.row];
}

return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isFilter)
{
_searchTextField.text=[searchArray objectAtIndex:indexPath.row];
idlbl.text=[idarray2 objectAtIndex:indexPath.row];

}
else
{
_searchTextField.text=[arrOfColor objectAtIndex:indexPath.row];
idlbl.text=[idarray2 objectAtIndex:indexPath.row];

}

}

-(void)textFieldDidChange:(UITextField *)textField
{
searchTextString=textField.text;
[self updateSearchArray:searchTextString];
}
-(void)updateSearchArray:(NSString *)searchText
{
if(searchText.length==0)
{
isFilter=NO;
}
else{

isFilter=YES;
searchArray=[[NSMutableArray alloc]init];
for(NSString *string in arrOfColor){

NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location !=NSNotFound){

[searchArray addObject:string];
}
}
[self.colorTableview reloadData];}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

enter image description here

数组搜索值为6之前

enter image description here

但是当我搜索它时,它的 ID 变为 1。

那么,我怎样才能在搜索后得到 6 id。提前致谢!

最佳答案

您必须创建一个新数组。示例代码:

NSMutableArray *arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil];  //Hear arrofColor is NSMutableArray.
NSMutableArray *idarray2 =[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];

NSMutableArray *searchArrayColor =[[NSMutableArray alloc]init];
NSMutableArray *searchArrayId =[[NSMutableArray alloc]init];


for(NSString *string in arrOfColor){

NSRange stringRange=[string rangeOfString:@"r" options:NSCaseInsensitiveSearch];
if(stringRange.location !=NSNotFound){
[searchArrayColor addObject:string];

NSInteger index = [arrOfColor indexOfObject:string];

[searchArrayId addObject:[idarray2 objectAtIndex:index]];
}
}

NSLog(@"%@",arrOfColor);

/*2016-10-14 12:15:44.618 objC[1855:50348] (
Red,
Green,
Blue,
Gray,
Black,
White,
Yellow,
Brown,
Pink
)*/

NSLog(@"%@",idarray2);

/*2016-10-14 12:15:44.619 objC[1855:50348] (
1,
2,
3,
4,
5,
6,
7,
8,
9
)*/

NSLog(@"===========");

NSLog(@"%@",searchArrayColor);
/*
2016-10-14 12:15:44.619 objC[1855:50348] (
Red,
Green,
Gray,
Brown
)
*/


NSLog(@"%@",searchArrayId);
/*
2016-10-14 12:15:44.619 objC[1855:50348] (
1,
2,
4,
8
)
*/

关于ios - 如何在 Objective c 的 UITableview 中获取 NSMutableArray 的实际值而不是索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40036478/

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