Init之後的CollectionView我有func collectionView
裏面class MoviesViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIScrollViewDelegate {
如何加載自定義函數
此功能的問題INIT自動的,我不希望自動啓動。或者在完成加載加載外部數據的函數時執行此操作。
這是ViewController.swift
class MyViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIScrollViewDelegate {
@IBOutlet var scrollView : UIScrollView!
@IBOutlet var collectionView1 : UICollectionView!
這是我的結構代碼的功能,我想之前CollectionView
func loaddata(){
let url: NSURL = NSURL(string: "http://www.mywebsite/testdata.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
let bodyData = "data=something"
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response, data, error) in
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
}
}
override func viewDidLoad() {
super.viewDidLoad()
loaddata()
}
override func viewDidLayoutSubviews() {
self.scrollView!.contentSize = CGSizeMake(1920, 2200)
}
這裏負荷,FUNC CollectionView
INIT自動
//this is function that load automatically
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0.0, left: 50.0, bottom: 0.0, right: 50.0)
}
// this is function load automatically
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if (collectionView == self.collectionView1) {
let cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell
let imageFilename = "featured-\(indexPath.row).jpg"
cell.featuredImage.image = UIImage(named: imageFilename)
return cell
}
return UICollectionViewCell()
}
我怎樣才能做保持ViewController
sta rt之前loadData
?或者直到ViewController
纔會啓動,直到您從loadData
調用該功能
爲什麼你在同一個視圖中都有滾動視圖和集合視圖? –
這是我從這裏得到的結構。它運作良好,但我需要改變它。 CollectionView在ScrollView中。這就是爲什麼自動啓動? – OscarM
'loadData()'函數完成獲取網絡數據後,您可以重新加載集合視圖。另一件事我沒有找到'-collectionView:numberOfItemsInSection:'的實現,它告訴集合視圖將渲染多少項目,當你沒有數據時它應該返回'0'。 –