gpt4 book ai didi

ios - 在 Collection View 中为 UImageView 添加标题标签

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:30 25 4
gpt4 key购买 nike

我是 iOS 开发的新手。我一生中从未写过一段代码。但我正在尝试遵循本教程 here .我已经成功完成了本教程,但我希望对代码进行一些修改。

  • 如何在每个加载的图像下添加文本“标题”?
  • 还有我该如何改变图像边框的颜色和应用背景?

View Controller :

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet var collectionView: UICollectionView!

var galleryItems: [GalleryItem] = []
var listOfDescriptions = [String]()

// MARK: -
// MARK: - View Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
initGalleryItems()
collectionView.reloadData()
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

private func initGalleryItems() {

var items = [GalleryItem]()
let inputFile = NSBundle.mainBundle().pathForResource("items", ofType: "plist")

let inputDataArray = NSArray(contentsOfFile: inputFile!)

for inputItem in inputDataArray as! [Dictionary<String, String>] {
let galleryItem = GalleryItem(dataDictionary: inputItem)
items.append(galleryItem)
}

galleryItems = items
}
private func populateList() {
listOfDescriptions.append("Valhaha is a flavor");
listOfDescriptions.append("Unicorns blood is made from dead unicorns. Harry Potter in this bit");
}
private func getDescription(position: Int) -> String {
return listOfDescriptions[position]
}

// MARK: -
// MARK: - UICollectionViewDataSource

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryItems.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GalleryItemCollectionViewCell", forIndexPath: indexPath) as! GalleryItemCollectionViewCell

cell.setGalleryItem(galleryItems[indexPath.row])
return cell

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

let commentView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "GalleryItemCommentView", forIndexPath: indexPath) as! GalleryItemCommentView

commentView.commentLabel.text = "Supplementary view of kind \(kind)"

return commentView
}

// MARK: -
// MARK: - UICollectionViewDelegate

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let alert = UIAlertController(title: "didSelectItemAtIndexPath:", message: "Indexpath = \(indexPath)", preferredStyle: .Alert)

let alertAction = UIAlertAction(title: "Dismiss", style: .Destructive, handler: nil)
alert.addAction(alertAction)

self.presentViewController(alert, animated: true, completion: nil)
}

// MARK: -
// MARK: - UICollectionViewFlowLayout

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let picDimension = self.view.frame.size.width / 4.0
return CGSizeMake(picDimension, picDimension)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let leftRightInset = self.view.frame.size.width / 14.0
return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset)
}
}

GalleryItemCollectionCellView:

import UIKit

class GalleryItemCollectionViewCell: UICollectionViewCell {

@IBOutlet var itemImageView: UIImageView!

func setGalleryItem(item:GalleryItem) {
itemImageView.image = UIImage(named: item.itemImage)
}

最佳答案

对于您的项目,您希望在 ImageView 下方添加标签,您应该像这样添加到 Storyboard上的单元格:

enter image description here

将导出拖到单元格:

enter image description here

改变setdata函数:

func setGalleryItem(item:GalleryItem) {
itemImageView.image = UIImage(named: item.itemImage)
itemNameLabel.text = item.itemImage
self.backgroundColor = UIColor.purpleColor()
}

然后在 viewController 中重新计算单元格的大小:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let picDimension = self.view.frame.size.width / 4.0
return CGSizeMake(picDimension, picDimension + 31)
}

根据您的需要,您可以将 31 更改为您想要的标签大小。

我看到你想要的边框是单元格的背景。所以将其添加到上面的设置项中。如果您只想在 ImageView 周围添加边框:您可以使用:

func setGalleryItem(item:GalleryItem) {
itemImageView.image = UIImage(named: item.itemImage)
itemNameLabel.text = item.itemImage
self.backgroundColor = UIColor.purpleColor()
itemImageView.layer.borderColor = UIColor.redColor().CGColor
itemImageView.layer.borderWidth = 1.0
}

选择你想要的等待。

更改添加到 viewDidload 中的 View 的颜色

  override func viewDidLoad() {
super.viewDidLoad()
initGalleryItems()
collectionView.reloadData()
self.view.backgroundColor = UIColor.lightGrayColor()//change color view
}

这是来自您的来源的演示:Demo

关于ios - 在 Collection View 中为 UImageView 添加标题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com