Getting Wrong id from NSMutableArray in objective c

丶灬走出姿态 提交于 2020-01-06 15:59:16

问题


I'm new in iOS and when I add search box in my textbox after searching it give me the wrong id. In my code there is three array one is for id another is for name and third one is to search.

My code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections{
    if(tableView == table)
    {
        if(isFilter)
        {
            return [searchArray count];
        }
        else
        return [idarray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == table)
    {
        if(isFilter)
        {
            cell.textLabel.text=[NSString stringWithFormat:@"%@",[searchArray objectAtIndex:indexPath.row]];
        }
        else
        {
            cell.textLabel.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(tableView == table)
        {
            if(isFilter)
            {
                txt.text=[searchArray objectAtIndex:indexPath.row];
                table.hidden=YES;
                txt.enabled=NO;
                txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
                idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
                EmployeeID=idlbl.text;
                EmployeeName=txt.text;

            }
            else
            {
                txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
                idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
                table.hidden=YES;
                EmployeeID=idlbl.text;
                EmployeeName=txt.text;
                txt.enabled=NO;
            }

        }
}

-(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 namearray){

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

                [searchArray addObject:string];
            }
        }
        [table reloadData];}
}

I'm getting the EmployeeID as 1 where EmployeeID is 18 It is taking the value which is in the table not taking the value of id array. I'm using if(tableview == table) because there is more then one table in one view controller. It is also updating the id. as shown in image

But I do not need to update the idarray. My question is how can I get Value of array it is giving me an indexpath of tableview cell Any Suggetion Thanks in Advance!


回答1:


When i used to work on the search bar button in my app, i too got same problem , but i have created the textbox which is acting as search bar.

i thought it will be work fine for you also, Happy Coding.......

click here




回答2:


Check this file for create one array and perform searching on that array with NSPredicate

https://gist.github.com/himanshu-benzatine/716fd8d7ca035a5c0a01c87df48915bf



来源:https://stackoverflow.com/questions/39993989/getting-wrong-id-from-nsmutablearray-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!