1

多くの場所を検索しましたが、答えが見つからなかったので、自分で質問をすることにしました。

UITableViewにJSONからのデータを入力します。私のデータ構造は次のようなものです。

NSMutableArray* studentList = [(NSDictionary*)[responseString JSONValue] objectForKey:@"StudentList"];

JSON:

{
"StudentList":[
    {
      "StudentID":"08DH11039",
      "StudentFirstName":"Nguyen Lam",
      "StudentLastName":"Binhh"
    },
    {
      "StudentID":"08DH11163",
      "StudentFirstName":"Nguyen Minh",
      "StudentLastName":"Duc"
    },
    {
      "StudentID":"08DH11038",
      "StudentFirstName":"Nguyen Bao",
      "StudentLastName":"Khuyen"
    },
    {
      "StudentID":"08DH11037",
      "StudentFirstName":"Nguyen Tran Duy",
      "StudentLastName":"Phuong"
    }
  ]
}

UITableViewにデータを追加します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        NSDictionary *student = [studentList objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%d. %@ %@", indexPath.row+1, [student objectForKey:@"StudentFirstName"], [student objectForKey:@"StudentLastName"]];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }

UISearchBarも追加しましたが、機能しませんでした。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [searchData removeAllObjects];

    NSArray *student; 

    for(student in studentListCopy) //take the n group (eg. group1, group2, group3)
    {
        NSLog(@"%@", student);
        NSMutableArray *newGroup = [[NSMutableArray alloc] init];
        NSString *element;

        for(element in student)
            NSRange range = [element rangeOfString:searchString
                                           options:NSCaseInsensitiveSearch];

            if (range.length > 0) { //if the substring match
                [newGroup addObject:element]; //add the element to group
            }
        }

        if ([newGroup count] > 0) {
            [searchData addObject:newGroup];
        }
    }

    return YES;
}

それを機能させるために私を助けてください。TableViewに表示されているFirstNameまたはLastNameを検索したい。

私はiOSプログラミングに不慣れです。どうもありがとうございます。

4

1 に答える 1

0

NSMutableArrayこれは、 が UISearchBar および UITableView でどのように使用されるかを理解するのに役立つと思います。インターネットから入手した例はこちらです。多分これはあなたの問題を解決するのに役立つでしょう

于 2012-07-04T12:19:53.573 に答える