UICollectionView的帮助我正在使用textview来显示内容。 Collectionview包含10个单元格。我无法根据textview的内容大小指定textview的高度。我一次显示一个单元格。在故事板中,textview高度为240px。但我需要根据内容大小扩展内容视图。我的代码如下。但是,如果我跑, 收到错误时出现:致命错误,而读取的read_access值为零。请指导我!!
// 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?
}
答案 0 :(得分:0)
首先需要注册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
}
}