Я новичок в iOS, но могу кодировать в Swift 3. Я вообще не знаю Objective-C.Ярлыки UICollectionViewCell перекрывают друг друга в цвете фона
Я изучаю UICollectionView
, и у меня есть набор строковых массивов. Когда я устанавливаю пользовательскую ширину в ячейку, она работает не так хорошо. ярлык перекрывает друг друга. После этого, когда я использую flowLayout, он обрезает метку. Кроме того, определенное количество 5 ячеек в одной строке исправлено.
Вот мой код:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let reuseIdentifier = "cell"
var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"];
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor.yellow
let a = (items[indexPath.item]).size(attributes: nil)
cell.frame.size.width = a.width + 20
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
flowLayout.minimumInteritemSpacing = 5.0
print("You selected cell #\((items[indexPath.item]).size(attributes: nil).width)!")
}
}
Вот мои скриншоты:
Наконец, я хочу вывода, как в каждой клетке не появляется в индивидуальном порядке не перекрываются и не подгонка этикетки. Если ширина метки для «Как вы?» есть х, то ячейка, содержащая эту метку должна быть шириной х + 20 (его просто предположение), и что метки или ячейки не должны перекрываться на любой другой клетке или этикетке
UPDATE 1
Здесь код что, если я изменю.
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let reuseIdentifier = "cell"
var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"];
var x:[Double] = []
var tempItems:[String] = []
var flag = true
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
let label: UILabel = (cell.viewWithTag(15) as! UILabel)
label.text = self.items[indexPath.item]
return cell
}
// collectionview layoutflow method. first this method call then other delagates method call.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let Labell : UILabel = UILabel()
Labell.text = self.items[indexPath.item]
let labelTextWidth = Labell.intrinsicContentSize.width
return CGSize(width: labelTextWidth + 12, height: 35)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
}
И выход такое же:
flowLayout.minimumInteritemSpacing = 5,0 комментарий этой линии и проверить его –
@HimanshuMoradiya подрезка не проблема. Но перекрывается.Я сделал обрезку, чтобы показать пример, что я пробовал много способов, но не работает –
Я немного путаюсь в вашей проблеме, просто обновляю ваш вопрос тем, что вы хотите получить. –