我遇到了UICollectionViewCell的问题,对我这样的新手来说真的很难。我需要你的帮助。 我正在写一本字典。我有一个搜索栏,一个集合视图,一些标签和一个文本视图。但是在显示搜索结果时,我无法调整集合视图单元格和文本视图的大小。
查看控制器:
import UIKit
class sozlukController: UIViewController, UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate{
@IBOutlet var wordSearchBar: UISearchBar!
@IBOutlet var collection: UICollectionView!
var wordDataList = [String]()
var meaningDataList = [String]()
var dictionaryDataList = [String]()
var yearDataList = [String]()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
wordDataList = []
meaningDataList = []
dictionaryDataList = []
yearDataList = []
var word = searchBar.text
var parser:dictXMLParser = dictXMLParser()
var searchResults:[dictXMLResponse]=parser.beginParsing(1, searchType: 1, searchWord: word)
for result in searchResults {
wordDataList.append(result.word)
meaningDataList.append(result.meaning)
dictionaryDataList.append(result.dictionary)
yearDataList.append(result.year)
}
searchBar.resignFirstResponder()
searchBar.text=""
collection.reloadData()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return wordDataList.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:meaningCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! meaningCollectionViewCell
cell.wordLabel.text = wordDataList[indexPath.row]
cell.meaningText.text = meaningDataList[indexPath.row]
cell.dictionaryLabel.text = dictionaryDataList[indexPath.row]
return cell
}
}
UICollectionViewCell:
import UIKit
class meaningCollectionViewCell: UICollectionViewCell , UITextViewDelegate{
@IBOutlet var wordLabel: UILabel!
@IBOutlet var meaningText: UITextView!
@IBOutlet var dictionaryLabel: UILabel!
override func layoutSubviews() {
super.layoutSubviews()
let fixedWidth = meaningText.frame.size.width
let newSize = meaningText.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
meaningText.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
}
}
当我写" gamze"在"第一个"的搜索栏中时间,因为文本视图高度,我没有看到整个意思。
令人惊讶的是,当我为第二次搜索编写相同的单词时,文本视图高度增加,并且它覆盖了以下标签。但是由于UICollectionViewCell高度,我再也看不到整个意义。
我设置了可滚动的" false"和自动布局" on"通过界面构建器。 有没有一种方法可以显示textview中的含义,它不包含任何标签,并根据其内容调整大小集合视图单元格
我无法找到有关swift的消息来源。如果你能提供帮助,我会非常感激。
答案 0 :(得分:0)
我在github上找到了一个很棒的解决方案。诀窍是为单元格内容创建xib文件
https://github.com/honghaoz/Dynamic-Collection-View-Cell-With-Auto-Layout-Demo