搜索栏在 Table 视图的两个部分中显示结果

Search Bar displaying results in both sections of Table View

我的 iOS 应用程序有问题,我在 table 视图中添加了 2 个部分,但是搜索栏在我的两个部分中显示了它的结果,而不是只显示一次.

这里只是猜测,但问题可能是因为搜索栏没有自己的部分,所以它将结果放在两个部分中?我必须添加另一个部分吗?修复代码?或者添加另一个 tableview/view controller 来处理 filteredArray?

代码:

dataArray 是我从服务器

获得所有数组的地方

followedArray 是某些数组来自 dataArray

的地方

filteredArray 是从 dataArray

中搜索到的数组

numberOfRowsInSection

if (!isFiltered) {

    if (section == 0) {
        return [followedArray count];
    }
    else {
        return [dataArray count];
    }
}
return [filteredArray count];

titleForHeaderInSection

if (section == 0) {
    return @"Followed Data";
}
else {
    return @"All Data";
}

cellForRowAtIndexPath

Data * dataObject;
if (!isFiltered) {

    if (indexPath.section == 0) {
        dataObject = [followedArray objectAtIndex:indexPath.row];
    }
    else {
        dataObject = [dataArray objectAtIndex:indexPath.row];
    }
}
else {
    dataObject = [filteredArray objectAtIndex:indexPath.row];
}

searchBar textDidChange

if (searchText.length == 0) {
    isFiltered = NO;
} else {
    isFiltered = YES;

    filteredArray = [[NSArray alloc] init];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self.dataName contains[c] %@", searchText];
    filteredArray = [dataArray filteredArrayUsingPredicate:resultPredicate];

}
[myTableView reloadData];

--------------------------------

我在这里做错了什么?还是我遗漏了什么?

我想在第 1 节中使用我的 followedArray,在第 2 节中使用 dataArray,并在使用搜索栏搜索 dataArray 时使用 filteredArray 以独立显示它,而不是像现在这样在两个部分中显示。谢谢!

答案很简单,但我是新手 iOS

// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {
            return "Followed Data"
        }
        else {
            return "All Data"
        }
    }

    return "Filtered Data"
}

// Number of Rows in Section
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {

            return followedArray.count
        }
        else if (section == 1) {

            return dataArray.count
        }
    }

    return filteredArray.count
}

// Number of Sections
func numberOfSections(in tableView: UITableView) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        return 2
    }

    return 1
}