2016-02-12 15 views
15

tôi đã sử dụng đoạn mã sau vào một UICollectionViewControllerKéo để làm mới trong UICollectionView trong ViewController

override func viewDidLoad() {    

    self.collectionView!.alwaysBounceVertical = true 
    let refresher = UIRefreshControl() 
    refresher.addTarget(self, action: "refreshStream", forControlEvents: .ValueChanged) 

    refreshControl = refresher 
    collectionView!.addSubview(refreshControl!) 
} 

func refreshStream() { 

    print("refresh") 
    self.collectionView?.reloadData() 

    refreshControl?.endRefreshing() 

} 

Bây giờ tôi cần nó để làm việc với một UICollectionView bên trong một UIViewController và tôi googled cho một giờ bây giờ nhưng không thể có được nó đang làm việc. Tôi đánh giá cao bất kỳ trợ giúp nào.

+0

Không phải là nó hoạt động? http://stackoverflow.com/questions/14678727/uirefreshcontrol-on-uicollectionview-only-works-if-the-collection-fills-the-heig – markov

+0

Mã chính là mục tiêu c và nhận xét nhanh chóng giúp tôi giải quyết được nhiều vấn đề : –

+0

Sử dụng số nhận dạng chưa được giải quyết 'Const' –

Trả lời

9

Từ bảng phân cảnh, bạn cần liên kết chế độ xem bộ sưu tập với tệp bộ điều khiển thích hợp, sử dụng Ctrl + Kéo từ đối tượng chế độ xem bộ sưu tập. Nó cần phải được liên kết thông qua một @IBOutlet.

Ngoài ra, bạn nên Ctrl + Kéo từ chế độ xem bộ sưu tập sang đối tượng bộ điều khiển chế độ xem trên bảng phân cảnh và chọn Nguồn dữ liệu và Ủy quyền để chế độ xem bộ sưu tập được liên kết chính xác.

Hãy cho tôi biết nếu điều này giúp hoặc nếu tôi đã hiểu lầm tình huống của bạn.

+0

địa ngục yeah! cảm ơn bạn. các cửa hàng dit nó. cảm ơn bạn! –

+0

hy vọng bạn sẽ nhận được nhiều phiếu bầu hơn nữa! tuyệt vời! –

11
var refreshControl:UIRefreshControl! 

override func viewDidLoad() { 
    super.viewDidLoad() 
     self.refreshControl = UIRefreshControl() 
     self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh") 
    self.refreshControl.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged) 
     collectionView!.addSubview(refreshControl) 
} 

func refresh(sender:AnyObject) 
    { 
    //DO 
    } 
+0

Mã của bạn muốn tôi đưa vào bộ sưu tập "ValueChanged" được chuyển tiếp một nửa semicolone! " ----> Các câu lệnh liên tiếp trên một dòng phải được phân cách bởi ';' –

+0

@ AliAB done :) –

+0

Đây là câu trả lời đúng –

30

đang nhanh chóng mới thay đổi trong phương pháp hành động kêu gọi bạn có thể làm như thế này viết lại

@IBOutlet weak var collectionView: UICollectionView! 

var refresher:UIRefreshControl! 

override func viewDidLoad() { 

    super.viewDidLoad() 

    self.refresher = UIRefreshControl() 
    self.collectionView!.alwaysBounceVertical = true 
    self.refresher.tintColor = UIColor.red 
    self.refresher.addTarget(self, action: #selector(loadData), for: .valueChanged) 
    self.collectionView!.addSubview(refresher) 
} 

func loadData() { 
    //code to execute during refresher 
     . 
     . 
     . 
    stopRefresher()   //Call this to stop refresher 
} 

func stopRefresher() { 
    self.refresher.endRefreshing() 
} 
+0

Cảm ơn bạn, nhưng "var refreshControl: UIRefreshControl!" cần thay đổi thành "var refresher: UIRefreshControl!" –

+4

chứ không phải là '' 'addSubview (bồi dưỡng)' '', cách nhanh hơn để bổ sung này vào bộ sưu tập là: '' 'self.collectionView !refreshControl = refresher self.collectionView! .refreshControl.beginRefreshing() self.collectionView !refreshControl.endRefreshing() '' ' – KingChintz

+1

@KingChintz collectionView.refreshControl chỉ khả dụng trong iOS10. addSubview hoạt động trên nhiều phiên bản hơn. –

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