从目标c中的NSMutableArray获取错误的ID [英] Getting Wrong id from NSMutableArray in objective c

查看:110
本文介绍了从目标c中的NSMutableArray获取错误的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS的新手,在搜索后在文本框中添加搜索框时,输入错误的ID.在我的代码中,有三个数组,一个数组用于ID,另一个数组用于名称,第三个数组用于搜索.

我的代码:

- (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];}
}

我将EmployeeID设为1,其中EmployeeID为18.它采用表中的值,而不采用id数组的值.我正在使用if(tableview == table),因为在一个视图控制器中有一个以上的表. 它还正在更新ID.如图所示

但是我不需要更新idarray.我的问题是如何获取数组的值,它为我提供了tableview单元格的索引路径 在此先感谢!

解决方案

检查该文件以创建一个数组,并使用NSPredicate在该数组上执行搜索

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

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!

解决方案

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

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

这篇关于从目标c中的NSMutableArray获取错误的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆