2016-07-13 16 views
5

Tôi muốn cho phép người dùng ứng dụng của tôi tìm kiếm hình ảnh bằng cách sử dụng UISearchBar phía trên một số UICollectionView. Theo sự hiểu biết của tôi, một số UICollectionView phải ở trong một số UICollectionViewController để hoạt động bình thường. Tuy nhiên, Xcode sẽ không cho phép tôi đặt thanh tìm kiếm trong một số UICollectionViewController. Tôi cũng không thể sử dụng chung UIViewController vì chế độ xem bộ sưu tập sẽ không hoạt động đúng cách. Làm thế nào tôi có thể đạt được các chức năng mà tôi muốn?Tôi làm cách nào để thêm thanh tìm kiếm phía trên UICollectionView?

+0

bạn tìm liên kết dưới đây, điều này có thể giúp bạn. [ví dụ-cách-tích hợp-uicollectionview-với-uisearchbar -1] (https://maniacdev.com/2015/01/example-how-to-integrate-uicollectionview-with-uisearchbar) – Chandan

Trả lời

1

Không bắt buộc phải có UICollectionView bên trong UICollectionViewController. UICollectionView giống như UITableView và có thể được thêm ở mọi nơi. Tất cả những gì bạn cần làm là thực hiện các giao thức UICollectionViewDelegateUICollectionViewDataSource. Bạn có thể làm theo hướng dẫn sau Supplementary Header và thêm thanh tìm kiếm làm tiêu đề bổ sung của UICollectionView.

+0

Tôi đã làm điều đó nhưng tôi đã nhận được NSExceptions mặc dù tôi đã triển khai UICollectionViewDelegate và UICollectionViewDataSource –

+0

Ngoại lệ là gì? –

18

Bộ sưu tậpXem + Tìm kiếmBar với Swift3 + Bảng phân cảnh triển khai.

Tạo Header Xem:

Creating the Header View

Tạo Search Bar:

Creating the Search Bar

Tạo quan điểm tái sử dụng lớp tùy chỉnh

Create the reusable view custom class

Đặt giao diện tùy chỉnh tái sử dụng lớp

Set the reusable view custom class

Tạo ổ cắm thanh tìm kiếm

Create the search bar outlet in custom class

Trick: Nối tìm kiếm thanh đại biểu đến lớp THU VIEW, ổ cắm thanh tìm kiếm đi vào XEM CLASS CUSTOM SÀI LẠI

Connect the search bar delegate to collection view class

Thực hiện các phương pháp tiêu đề CollectionView của giao thức

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

     if (kind == UICollectionElementKindSectionHeader) { 
      let headerView:UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CollectionViewHeader", for: indexPath) 

      return headerView 
     } 

     return UICollectionReusableView() 

    } 

Đặt các đại biểu Searchbar

class MyCollectionViewController: (other delegates...), UISearchBarDelegate { 

Và cuối cùng, các phương pháp đại biểu thanh tìm kiếm của bạn sẽ được gọi trong CollectionView Lớp

//MARK: - SEARCH 

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
    if(!(searchBar.text?.isEmpty)!){ 
     //reload your data source if necessary 
     self.collectionView?.reloadData() 
    } 
} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
    if(searchText.isEmpty){ 
     //reload your data source if necessary 
     self.collectionView?.reloadData() 
    } 
} 
+4

Thật không may cách tiếp cận này có một vài tác dụng phụ; thanh tìm kiếm sẽ xuất hiện trong mọi tiêu đề phần, thanh tìm kiếm sẽ cuộn ra khỏi chế độ xem và thanh tìm kiếm sẽ mất trạng thái firstResponder khi self.collectionView? .reloadData() được gọi. Kiểm tra Jeremiah-de tuyệt vời từng bước repo để sử dụng UISearchController với UICollectionView https://github.com/jeremiah-de/CollectionViewSearch/tree/basic-collection-view – aumansoftware

Các vấn đề liên quan