2017-07-06 78 views
1

使用Swift 3,该应用程序是一个使用PHP和JSON从MYSQL数据库中读取的博客阅读器。Swift:通过两个部分搜索栏搜索,而不是将它们组合

目前我的SearchBar没有做我想做的事情,我在mainArray(第1部分)中搜索'All'作用域。当它被过滤时,被过滤的对象将被移动到filteredArray。我同时做到了这一点,因为我无法弄清楚如何让它做我想做的事情。

它应该做的是这样的,当用户搜索一个对象时,我希望该对象显示在mainArray或followedArray中,而不是将它移动到另一个不同的数组,以便它不合并。基本上过滤tableview并且不删除任何部分或组合任何对象,因为它会通过不知道对象在哪个部分中而使用户混淆,并且当然确保范围条正常工作。

了解如何实施搜索栏,以便在我尝试进一步提升级别时看到我的麻烦。谢谢!

搜索栏&范围代码

let searchController = UISearchController(searchResultsController: nil) 

override func viewDidLoad() { 
    // Search Bar 
    searchController.searchResultsUpdater = self 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 
    myTableView.tableHeaderView = searchController.searchBar 
    searchController.searchBar.backgroundColor = UIColor.white 
    searchController.searchBar.barTintColor = UIColor.white 

    // Scope Bar 
    searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Free"] 
    searchController.searchBar.delegate = self 
} 

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

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

     if section == 0 { 
      return "Followed Blogs" 
     } 
     else { 
      return "All Blogs" 
     } 
    } 
    return "Filtered Blogs" 
} 

// Number of Rows in Section 
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 mainArray.count 
     } 
    } 
    return filteredArray.count 
} 

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

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

     return 2 
    } 
    return 1 
} 

// CellForRowAt indexPath 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let CellIdentifier = "Cell" 
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell 

    if cell != cell { 
     cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) 
    } 

    // Configuring the cell 
    var blogObject: Blog 

    if !(searchController.isActive && searchController.searchBar.text != "") { 
     if indexPath.section == 0 { 
      blogObject = followedArray[indexPath.row] 
      cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self) 
     } 
     else if indexPath.section == 1 { 
      blogObject = mainArray[indexPath.row] 
      cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) 
     } 
    } 
    else { 
     blogObject = filteredArray[indexPath.row] 
     cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) 
    } 

    return cell 
} 

// SEARCH BAR: Filtering Content 
func filterContentForSearchText(searchText: String, scope: String = "All") { 

    filteredArray = mainArray.filter { Blog in 

     let categoryMatch = (scope == "All") || (Blog.blogType == scope) 

     return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) 
    } 
    myTableView.reloadData() 
} 

// SEARCH BAR: Updating Results 
func updateSearchResults(for searchController: UISearchController) { 

    filterContentForSearchText(searchText: searchController.searchBar.text!) 
} 

// SEARCH BAR: Scope 
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 

    filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) 
} 

// SEARCH BAR: Updating Scope 
func updateSearchResultsForSearchController(searchController: UISearchController) { 

    let searchBar = searchController.searchBar 
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] 
    filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope) 
} 

// Deallocating Search Bar 
deinit{ 
    if let superView = searchController.view.superview { 
     superView.removeFromSuperview() 
    } 
} 

回答

2

现在你创建一个单一的阵列(filteredArray),并假设你有1段搜索时。

我会删除这个假设。

在您的filterContentForSearchText方法中,创建一个数组数组(其中每个内部数组表示一个部分)。

然后更新所有的表视图数据源方法以使用该数组数组来获取正确的值。

首先,将您的声明filteredArray更新为数组数组。

现在更新表格视图方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if !(searchController.isActive && searchController.searchBar.text != "") { 
     if section == 0 { 
      return followedArray.count 
     } else { 
      return mainArray.count 
     } 
    } else { 
     return filteredArray[section].count 
    } 
} 

// Number of Sections 
func numberOfSections(in tableView: UITableView) -> Int { 
    if !(searchController.isActive && searchController.searchBar.text != "") { 
     return 2 
    } else { 
     return filteredArray.count 
    } 
} 

// CellForRowAt indexPath 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let CellIdentifier = "Cell" 
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell 

    if cell != cell { 
     cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) 
    } 

    // Configuring the cell 
    var blogObject: Blog 

    if !(searchController.isActive && searchController.searchBar.text != "") { 
     if indexPath.section == 0 { 
      blogObject = followedArray[indexPath.row] 
      cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self) 
     } else { 
      blogObject = mainArray[indexPath.row] 
      cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) 
     } 
    } else { 
     blogObject = filteredArray[indexPath.section][indexPath.row] 
     cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) 
    } 

    return cell 
} 

最后更新filterContentForSearchText方法:

func filterContentForSearchText(searchText: String, scope: String = "All") { 
    let filteredFollowed = followedArray.filter { Blog in 
     let categoryMatch = (scope == "All") || (Blog.blogType == scope) 

     return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) 
    } 

    let filteredMain = mainArray.filter { Blog in 
     let categoryMatch = (scope == "All") || (Blog.blogType == scope) 

     return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) 
    } 

    filteredArray = [ filteredFollowed, filteredMain ] 

    myTableView.reloadData() 
} 
+0

感谢伟大的详细的解答!我得到了一些错误,在'numberOfRowsInSection'' return filteredArray [indexPath.section] .count'它说'使用未解析的标识符'indexPath''。另外,当你说创建一个数组的数组,你的意思是这样吗? 'var filteredArray = [[Blog]]()' – BroSimple

+0

糟糕 - 将'indexPath.section'改为'section'。我更新了答案。 – rmaddy

+0

是的,这将是一个'Blog'数组的数组。 – rmaddy