我的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的信息时遇到了麻烦。如果您能提供帮助,我将不胜感激。
我在github上找到了一个很好的解决方案。诀窍是为单元格内容创建xib文件
https://github.com/honghaoz/Dynamic-Collection-View-Cell-With-Auto-Layout-Demo
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句