gpt4 book ai didi

ios - 具有自定义单元格的 TableView 重复包含图像的第一个和最后一个单元格

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:28 27 4
gpt4 key购买 nike

我在名为 ProductHomeCell 的自定义单元格类中的 ScrollView 内显示图像时遇到问题。当我向下滚动时,最后一个单元格的水平 ScrollView 中包含的图像被复制到第一个单元格中。这是填充每个自定义单元格的方法。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("productCell", forIndexPath: indexPath) as! ProductHomeCell

cell.productName.text = productImages[indexPath.row].name
cell.productPrice.text = "$"+productImages[indexPath.row].price
cell.matchLbl.text = "\(productImages[indexPath.row].match)"

// cell.PageImages contains Array of Images for scrollView
cell.pageImages = productImages[indexPath.row].imageSet

return cell

}

这里是 ProductHomeCell 的一些属性和方法,它们使用发送的图像和其他属性设置 ScrollView 。

 import Foundation
import UIKit


class ProductHomeCell: UITableViewCell, UIScrollViewDelegate {


@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var matchLabel: UILabel!

@IBOutlet weak var matchLbl: UILabel!


@IBOutlet weak var productName: UILabel!

@IBOutlet weak var comprarBtn: UIButton!

@IBOutlet weak var productPrice: UILabel!



var pageViews: [UIImageView?] = []
var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(60, 300, 200, 5))

var pageImages : [UIImage] = [] {

didSet {

let pageCount = pageImages.count
self.pageControl.numberOfPages = pageCount
configurePageControl()
pageControl.enabled = false

// 3
for _ in 0..<pageCount {
pageViews.append(nil)
}

// 4
let pagesScrollViewSize = scrollView.frame.size


scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
height: pagesScrollViewSize.height)

if pageCount == 1 {

self.pageControl.hidden = true

}

loadVisiblePages()





}


}


override func awakeFromNib() {



super.awakeFromNib()
scrollView.delegate = self
pageControl = UIPageControl(frame: CGRectMake(240, 0, 107, 37))
likeButton.setImage(UIImage(named:"corazon_borde_rojo"),forState:UIControlState.Normal)

comprarBtn.layer.borderColor = UIColor.lightGrayColor().CGColor
comprarBtn.layer.borderWidth = 0.5
comprarBtn.layer.cornerRadius = 12.0
comprarBtn.clipsToBounds = true

// 4
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight);
scrollView.minimumZoomScale = minScale;

// 5
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale;




}

func loadPage(page: Int) {
if page < 0 || page >= pageImages.count {
// If it's outside the range of what you have to display, then do nothing
return
}

// 1
if let pageView = pageViews[page] {
// Do nothing. The view is already loaded.
} else {
// 2
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.y = 0.0

// 3
let newPageView = UIImageView(image: pageImages[page])
// let newPageView = UIImageView(image: imageC)

newPageView.contentMode = .ScaleAspectFit
newPageView.frame = frame

scrollView.addSubview(newPageView)

// 4
pageViews[page] = newPageView
}
}

func loadVisiblePages() {
// First, determine which page is currently visible
let pageWidth = scrollView.frame.size.width
let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))

// Work out which pages you want to load
let firstPage = page - 1
let lastPage = page + 1

// Purge anything before the first page
for var index = 0; index < firstPage; ++index {
purgePage(index)
}

// Load pages in our range
for index in firstPage...lastPage {
loadPage(index)
}

// Purge anything after the last page
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}

func scrollViewDidScroll(scrollView: UIScrollView) {
// Load the pages that are now on screen
loadVisiblePages()
}

func purgePage(page: Int) {
if page < 0 || page >= pageImages.count{
// If it's outside the range of what you have to display, then do nothing
return
}

// Remove a page from the scroll view and reset the container array
if let pageView = pageViews[page] {
pageView.removeFromSuperview()

pageViews[page] = nil
}
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}


func configurePageControl() {

self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.blackColor()

self.pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
self.pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
self.addSubview(pageControl)
}

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}

@IBOutlet weak var likeButton: UIButton!


@IBAction func darLike(sender: AnyObject) {

if((likeButton.imageView?.image?.isEqual(UIImage(named: "corazon_borde_rojo"))) != nil){
likeButton.setImage(UIImage(named:"corazon_rojo"),forState: UIControlState.Normal)



}

}

@IBAction func verDetail(sender: AnyObject) {



}


override func prepareForReuse() {
super.prepareForReuse()

loadVisiblePages()

}





}

此类还包含设置 ScrollView 和 pageControl 与相应图像水平设置的方法。

我提到的问题是,当我向下滚动最后一个单元格中的图像时,会替换第一个单元格的图像,但不会替换价格或描述等属性。只有图像。谢谢!

最佳答案

您的单元格被重复使用,因此请确保在设置单元格之前清除所有值。

关于ios - 具有自定义单元格的 TableView 重复包含图像的第一个和最后一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319465/

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