【问题标题】:How to get actual value of NSMutableArray not Index in UITableview in Objective c如何在Objective c的UITableview中获取NSMutableArray的实际值而不是索引
【发布时间】:2017-02-23 11:47:26
【问题描述】:

我是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;
}

数组的搜索值为6之前

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

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

【问题讨论】:

  • 你是如何打印那个 id 的?
  • @HimanshuMoradiya 但是那个问题并没有解决我的问题,这就是我问的原因。
  • @Lion 赞这个 idlbl.text=[idarray2 objectAtIndex:indexPath.row];
  • 你已经问过这个问题,所以它是重复的,我已经给你这个问题的完美解决方案

标签: ios objective-c arrays xcode uitableview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    所以你显示id 喜欢,

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

    正如您在问题中的评论中所说,它肯定会打印出1,因为在搜索您的tableview 后只有one row 所以,indexPath.row 将是0,所以[idarray2 objectAtIndex:indexPath.row]; 表示第一个对象idarray21。所以,它会返回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; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 2022-11-17
      • 2018-08-05
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多