我有包含表格 View 和搜索栏的 View 。当我开始搜索时,它应该搜索表格 View 。下面是附图中表格 View 的示例数据:
学生 1、学生 2 是表中的不同部分
假设我搜索“S”,则结果应如下所示,其中匹配的行应可见,不匹配的行应隐藏,并且“显示剩余”按钮应可见。当我单击“显示剩余”按钮时,它应该显示同一部分的剩余行:
我如何实现这一点,如果可能,请提供上述场景的示例。
最佳答案
我已根据您的要求创建了代码,请找到以下网址下载代码并查看它。
链接:https://www.dropbox.com/s/22ijwgxybn98wyj/ExpandCollapse.zip?dl=0
Environment : Xcode 8 and Objective-C
突出显示我所做的代码。
我根据您的结构创建了学生的NSObject,创建两个(StudentListCell和ShowMoreCell)UITableViewCell
用于显示学生的记录和“显示剩余按钮”记录。
我还使用了两个(arrStudentInfo, arrSearchInfo) NSMutablebleArray
来保存搜索记录和原始记录。
之后我在ViewController.m
中初始化了student的NSObject。请找到下面的代码。
- (void)setupData {
self.arrStudentInfo = [[NSMutableArray alloc] init];
self.arrSearchInfo = [[NSMutableArray alloc] init];
StudentInfo *student_1 = [[StudentInfo alloc] init];
student_1.name = @"Sia S";
student_1.style = @"S";
student_1.trend = @"S";
student_1.age = @"60";
student_1.locale = @"Node";
student_1.street = @"BR";
student_1.city = @"JP";
student_1.country = @"US";
student_1.isOpen = YES;
student_1.isShowMore = NO;
[self.arrStudentInfo addObject:student_1];
StudentInfo *student_2 = [[StudentInfo alloc] init];
student_2.name = @"Nitin";
student_2.style = @"N";
student_2.trend = @"N";
student_2.age = @"40";
student_2.locale = @"Node";
student_2.street = @"BR";
student_2.city = @"SP";
student_2.country = @"US";
student_2.isOpen = YES;
student_2.isShowMore = NO;
[self.arrStudentInfo addObject:student_2];
}
我创建了返回当前事件的方法,这意味着搜索记录或数组的原始记录。
- (NSMutableArray *)getCurrentArray {
if(self.isSearch)
return self.arrSearchInfo;
else
return self.arrStudentInfo;
}
根据场景加载数据,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
StudentInfo *studentInfo = [self getCurrentArray][indexPath.section];
if(indexPath.row == SHOW_MORE_INDEX && studentInfo.isShowMore == NO) {
static NSString *strCellIdentifier = @"ShowMoreCell";
ShowMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier];
cell.btnShowMore.tag = indexPath.section;
[cell.btnShowMore addTarget:self action:@selector(clickONShowMore:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
else {
static NSString *strCellIdentifier = @"StudentListCell";
StudentListCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier];
[cell setupCell:studentInfo indexPath:indexPath];
return cell;
}
}
要搜索学生姓名,
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
self.isSearch = YES;
NSPredicate *predicateStudent = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"self.name CONTAINS[cd] '%@'",searchBar.text]];
NSArray *arrGetSearch = [self.arrStudentInfo filteredArrayUsingPredicate:predicateStudent];
if(self.arrSearchInfo.count)
[self.arrSearchInfo removeAllObjects];
[self.arrSearchInfo addObjectsFromArray:arrGetSearch];
[self.tblView reloadData];
}
如果用户点击UISearchbar
上的“X”按钮,则再次加载原始记录。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if([searchText isEqualToString:@""] || searchText==nil) {
[searchBar resignFirstResponder];
self.isSearch = NO;
[self.tblView reloadData];
}
}
希望这对您有用。
关于ios - 具有行展开和折叠选项的 TableView 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152516/