ios - 具有行展开和折叠选项的 TableView 的最佳方法

标签 ios objective-c uitableview tableview customization

我有包含表格 View 和搜索栏的 View 。当我开始搜索时,它应该搜索表格 View 。下面是附图中表格 View 的示例数据:enter image description here

学生 1、学生 2 是表中的不同部分

假设我搜索“S”,则结果应如下所示,其中匹配的行应可见,不匹配的行应隐藏,并且“显示剩余”按钮应可见。当我单击“显示剩余”按钮时,它应该显示同一部分的剩余行:

enter image description here

我如何实现这一点,如果可能,请提供上述场景的示例。

最佳答案

我已根据您的要求创建了代码,请找到以下网址下载代码并查看它。

链接: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/

相关文章:

ios - 如何避免获取关系中的子实体

ios - NSInvocation 返回值但使用 EXC_BAD_ACCESS 使应用程序崩溃

ios - 仅通过电子邮件检查用户是否已有帐户

ios - 带有自定义 header 的 AWSS3GetPreSignedURLRequest 上传,给出 403

ios - UITableViewCell 作为来自 XIB 的 UIView

objective-c - 自定义UITabBar的外观

ios - 使用 MPMoviePlayerController 播放 mp3 和 mp4 文件

ios - 条件绑定(bind)中的绑定(bind)值必须是可选类型

ios - 将自定义的 UITableViewCell 导出到 UIImage

ios - 将数据从单元格加载到 detailView 始终显示最后的数据