【发布时间】:2015-07-30 09:52:13
【问题描述】:
我遇到了 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”自动布局设置为“on”。 有没有一种方法你知道在 textview 中显示含义,它不覆盖任何标签并根据其内容调整集合视图单元格的大小
我无法找到有关 swift 的资源。如果您能提供帮助,我将不胜感激。
【问题讨论】:
标签: ios swift uicollectionview uitextview uicollectionviewcell