【问题标题】:How to access cell property dynamically in uicollectionview in swift如何快速在uicollectionview中动态访问单元格属性
【发布时间】:2014-12-30 08:55:17
【问题描述】:

UICollectionView 的帮助 我正在使用 textview 来显示内容。 Collectionview 包含 10 个单元格。我无法根据该 textview 的内容大小指定 textview 的高度。我一次显示一个单元格。在故事板中,textview 高度为 240px。但我需要根据内容大小扩展内容视图。我的代码如下。但如果我跑步,
错误接收为:FATAL ERROR WHILE UNWRAPPING reading_access has nil value。请指导我!

// MAIN CLASS

class read_page: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate 
{



 @IBOutlet var reading_collection_view: UICollectionView!

 override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()


 }



func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: 
 Int) -> Int {

    return 10  
}

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

  var cell = story_collection_view.dequeueReusableCellWithReuseIdentifier("story_read", 
  forIndexPath: indexPath) as story_reading_cell

          cell.reading_area_txtvw.frame.size.height = 750

  return cell
}

// This code is working. But, from 3rd cell onwards it is working. When we compile,  
// textview does not change. After scrolling two more cells, it is working. How to get 
// its size from first cell onwards? How to change cell size?




}

【问题讨论】:

    标签: swift ios8 uicollectionview uitextview


    【解决方案1】:

    您首先需要注册 collectionViewCell 然后在 cellForItemAtIndexPath 中您可以使单元格出列。

    我也不会使用 UIViewController,而是使用 UICollectionViewController,因为它提供了默认委托和数据源实现。

    class read_page: UICollectionViewController {
    
        @IBOutlet var reading_collection_view: UICollectionView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            reading_collection_view.registerClass(reading_cell.self , forCellWithReuseIdentifier: "myCell")
        }
    
    //MARK: UICollectionViewDataSource
    
        override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 1
        }
    
        override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let myCell = reading_collection_view.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as reading_cell
            myCell.reading_area_txtvw.frame.size.height = myCell.reading_area_txtvw.contentSize.height
            return myCell
    
        }
    }
    

    【讨论】:

    • 线程 1:EXC_BAD INSRUCTION(CODE = EXC_1386) 错误:致命错误:在展开可选值时意外发现 nil
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多