2015-11-16 120 views
0

主控制器細胞不顯示 - SWIFT 2.1

class MainController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { 

@IBOutlet weak var collectionView: UICollectionView! 

let reUseCellName = "imgCell" 

var counter = 1 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reUseCellName, forIndexPath:indexPath) as! CellClass 

     cell.imageView.image = UIImage(named: "\(counter)") 
     cell.imgName = counter 
     counter++ 
     return cell 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {  
let newView = segue.destinationViewController as! showImage 
let cell = sender as! CellClass   
    newView.imgNo = cell.imgName   
} 

Storyboard

+0

細胞不顯示或在細胞中的圖像不顯示? – anhtu

+0

var counter = 1因此圖像名稱爲「1」使用圖像名稱爲1.png或1.jpg –

+0

@anhtu都不顯示。只顯示滾動條 –

回答

0

我認爲它你如何使用它們,請嘗試使用圖像定義數組的方式:

cellForItemAtIndexPath
var arrayOfImages = ["1","2","3","4"] 

並使用它:

cell.imageView.image = UIImage(named: arrayOfImages[indexPath.row]) 

請確保將DataSoruceDelegate連接到連接檢查器的collectionView到您的視圖控制器以加載數據,並確保放置正確的圖像名稱。

0

使用下面的代碼。不顯示圖像的原因是你沒有提到圖像的文件擴展名爲JPG或PNG ...你的計數器值是「1」,所以應用程序無法加載圖像名稱1沒有擴展名PNG或JPG 使用此代碼:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reUseCellName, forIndexPath:indexPath) as! CellClass 

    cell.imageView.image = UIImage(named: "Complete image name with .png/.jpg") 
    cell.imgName = counter 
    counter++ 
    return cell 

}