2016-10-14 37 views
0

我是iOS中的新手,我在搜索後遇到了有關獲取數組ID的問題。我需要在搜索後得到數組的實際值,但是當我搜索數組的內容時,它會給出當前在tableview中的數組索引。我的代碼是這樣的:如何獲得實際價值的NSMutableArray不索引目標c中的UITableview中

- (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。提前致謝!

+0

[目標C從NSMutableArray中獲取錯誤ID]的可能的複製(http://stackoverflow.com/questions/39993989/getting -wrong-id-from-nsmutablearray-in-objective-c) –

+0

如何打印該ID? – Lion

+0

@HimanshuMoradiya但這個問題沒有解決我的問題,這就是爲什麼我問。 – Muju

回答

2

你必須創建一個新的數組。 一個示例代碼:

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 
) 
*/ 
+0

非常感謝。你的代碼工作完美。 – Muju

2

所以就表明id一樣,

idlbl.text=[idarray2 objectAtIndex:indexPath.row]; 

,你在你的問題評論說,那麼它肯定會打印1因爲畢竟搜索您tableview只有one row所以,indexPath.row將爲0,所以[idarray2 objectAtIndex:indexPath.row];表示idarray2的第一個對象,它是1。所以,它會返回1

現在如果你想相關的ID,那麼你可以這樣做,

NSUInteger index = [arrOfColor indexOfObject:@"white"]; // pass color here i have take static value for demonstration you should pass dynamic value depend on search 


    NSString *yourId = [idarray2 objectAtIndex:index]; 

    idlbl.text=yourId; 
+0

謝謝您的代碼也運行。 – Muju

+0

不客氣..... :) – Lion

相關問題