Search code examples
arraysxcodeuitableviewuisearchbarsections

Search Bar displaying results in both sections of Table View


I have a problem in my iOS app, I added 2 sections to my table view but, the search bar is displaying its results in both of my sections instead of just once over all.

Just guessing here, but may the problem be because the search bar doesn't have it's own section so it places the results in both? Do I have to add another section? Fix the code? Or add another tableview/ view controller to handle the filteredArray?

Code:

dataArray is where I have all my arrays from my server

followedArray is where certain arrays go from dataArray

filteredArray is the searched arrays from 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];

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

What am I doing wrong here? Or am I missing something?

I want to have my followedArray in Section 1, dataArray in Section 2, and the filteredArray when searching the dataArray with the Search Bar to display it independently and not in both sections like how it's doing it now. Thank you!


Solution

  • Very simple answer but I was new to 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
    }