好吧,可以说,您的模型类具有您提到的以下属性,
例如在Student.h 文件中
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *name; //i am changing it to strong
@property (nonatomic, strong) NSString *address;
@property (strong, nonatomic) NSArray *teachers;
- (id)initWithName:(NSString *)name trachers:(NSArray *)teachers address:(NSString *)address;
@end
在Student.m 文件中
#import "Student.h"
@implementation Student
//custom initialiser
- (id)initWithName:(NSString *)name trachers:(NSArray *)teachers address:(NSString *)address
{
if(self = [super init])
{
self.name = name;
self.teachers = teachers;
self.address = address;
}
return self;
}
@end
在 ViewController.m 文件中,我正在创建 Student 的实例并将其添加到数组中,例如,
在ViewController.h 文件中,
#import <UIKit/UIKit.h>
#import "Student.h"
@property (nonatomic, strong) NSMutableArray *students; //to hold student instances
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;//to search the teachers
@property (weak, nonatomic) IBOutlet UITextView *textView;//to display the output
@end
在ViewController.m 文件中,
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_students = [[NSMutableArray alloc] init];
for(int k = 0 ; k < 5 ; k++)
{
NSMutableArray *teachers = [self getTeachersForStudent:k];//a helper method to fill teacher's name
Student *stud = [[Student alloc] initWithName:[NSString stringWithFormat:@"student_%d",k] trachers:teachers address:[NSString stringWithFormat:@"address_%d",k]]; //hear student name will be like "student_0"
[_students addObject:stud]; //finally add it to students array
}
}
//this is the helper method, for testing u can add different same name if you want
- (NSMutableArray *)getTeachersForStudent:(int)k
{
NSMutableArray *array = [[NSMutableArray alloc] init];
switch (k)
{
case 0:
[array addObjectsFromArray:@[@"anoop",@"shan",@"sara",@"peter",@"david",@"gopi"]];
break;
case 1:
[array addObjectsFromArray:@[@"hema",@"isah",@"sara",@"john"]];
break;
case 2:
[array addObjectsFromArray:@[@"venu",@"giri",@"kim",@"madu",@"shan"]];
break;
case 3:
[array addObjectsFromArray:@[@"venu",@"tom",@"yesh",@"vijay",@"shan"]];
break;
case 4:
[array addObjectsFromArray:@[@"vim",@"janu",@"jai",@"kiran",@"shan"]];
break;
default:
break;
}
return array;
}
最后将ViewController.m文件中的排序功能添加如下方法,它将从搜索栏中获取初始字符串并搜索所有教师姓名,最后给出将显示在文本视图中的学生姓名
- (void)refreshStudentList
{
__block NSMutableArray *result = [[NSMutableArray alloc] init];
[_students enumerateObjectsUsingBlock:^(Student* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
__block Student *presentStudent = obj;
__block NSArray *array = [presentStudent valueForKey:@"teachers"];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if([obj rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[result addObject:presentStudent];
}
}];
}];
//at this point result array contains all the student student name of the teacher that u are searched
NSString *resultString = @"";
for(Student *s in result)
{
resultString = [NSString stringWithFormat:@"%@ \n %@", resultString, s.name];
}
_textView.text = resultString;
}
在同一ViewController.m 中添加searhbar 委托方法并调用refreshStudentList 方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self refreshStudentList];
if (searchText.length == 0)
_textView.text = @""; //simply resetting the text view
}
最后的输出将如下所示