我在collectionview
中有一个tableView
。一切正常,直到我向下滚动tableview
并再次向上滚动我搜索过的collectionview
更改的图像,由于Dequereusable
单元格,我知道它的变化,但是我找不到任何处理方法为此,我正在通过网址使用Kingfisher
到Download image
。这是我的问题link的视频。
TableView
的代码
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 5
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if indexPath.row == 0{
let TableCell = tableView.dequeueReusableCell(withIdentifier: "TabelCell") as! HomeTableViewCell
// let TableCell = tableView.cellForRow(at: IndexPath(item: 0, section: 0) ) as! HomeTableViewCell
//let TableCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: nil) as! HomeTableViewCell
TableCell.MainCellTitleLabel.text = MainCellTitleArray[indexPath.row]
TableCell.HomeCollectionView.tag = indexPath.row
DispatchQueue.main.async() { () -> Void in
if self.NewsArray.count != 0
{
TableCell.HomeCollectionView.reloadData()
// check = false
}
}
return TableCell
}else if indexPath.row == 1{
let TableCell = tableView.dequeueReusableCell(withIdentifier: "TabelCell2") as! HomeTableViewCell
// let TableCell = tableView.cellForRow(at: indexPath) as! HomeTableViewCell
//let TableCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "TabelCell")
TableCell.MainCellTitleLabel.text = MainCellTitleArray[indexPath.row]
TableCell.HomeCollectionView.tag = indexPath.row
if NewsArray.count != 0
{
TableCell.HomeCollectionView.reloadData()
check = false
}
return TableCell
}else if indexPath.row == 2{
let TableCell = tableView.dequeueReusableCell(withIdentifier: "TabelCell3") as! HomeTableViewCell
// let TableCell = tableView.cellForRow(at: indexPath) as! HomeTableViewCell
//let TableCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "TabelCell")
TableCell.MainCellTitleLabel.text = MainCellTitleArray[indexPath.row]
TableCell.HomeCollectionView.tag = indexPath.row
if NewsArray.count != 0
{
TableCell.HomeCollectionView.reloadData()
check = false
}
return TableCell
}else if indexPath.row == 3{
let TableCell = tableView.dequeueReusableCell(withIdentifier: "TabelCell4") as! HomeTableViewCell
// let TableCell = tableView.cellForRow(at: indexPath) as! HomeTableViewCell
//let TableCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "TabelCell")
TableCell.MainCellTitleLabel.text = MainCellTitleArray[indexPath.row]
TableCell.HomeCollectionView.tag = indexPath.row
if NewsArray.count != 0
{
TableCell.HomeCollectionView.reloadData()
check = false
}
return TableCell
}
else{
let TableCell = tableView.dequeueReusableCell(withIdentifier: "TabelCell5") as! HomeTableViewCell
TableCell.MainCellTitleLabel.text = MainCellTitleArray[indexPath.row]
TableCell.HomeCollectionView.tag = indexPath.row
if NewsArray.count != 0
{
TableCell.HomeCollectionView.reloadData()
check = false
}
return TableCell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 190
}
CollectionView
的代码
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if collectionView == self.MainCollectionView
{
return SliderImagesArray.count
}else{
var size = Int()
if collectionView.tag == 0
{
size = NewsArray.count
}else if collectionView.tag == 1 {
size = EventsArray.count
}else if collectionView.tag == 2 {
size = PostArray.count
}else if collectionView.tag == 3 {
size = VideosArray.count
}else if collectionView.tag == 4 {
size = ResearchArray.count
}
return size
}
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
if collectionView == self.MainCollectionView{
let CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCell", for: indexPath) as! HomeCollectionViewCell
if SliderImagesArray.count != 0 {
if let imgUrl = URL(string: "\(ImageBaseUrl)\(SliderImagesArray[indexPath.item])")
{
CollectionCell.MainCollectionImageView.kf.indicator?.startAnimatingView()
CollectionCell.MainCollectionImageView.kf.setImage(with: imgUrl)
}
}
return CollectionCell
}else{
let CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! HomeCollectionViewCell
CollectionCell.contentView.layer.cornerRadius = 2.0
CollectionCell.contentView.layer.borderWidth = 1.0
CollectionCell.contentView.layer.borderColor = UIColor.clear.cgColor
CollectionCell.contentView.layer.masksToBounds = true
CollectionCell.layer.shadowColor = UIColor.black.cgColor
CollectionCell.layer.shadowOffset = CGSize(width: 0, height: 2.0)
CollectionCell.layer.shadowRadius = 2.0
CollectionCell.layer.shadowOpacity = 0.5
CollectionCell.layer.masksToBounds = false
CollectionCell.layer.shadowPath = UIBezierPath(roundedRect: CollectionCell.bounds, cornerRadius: CollectionCell.contentView.layer.cornerRadius).cgPath
CollectionCell.tag = indexPath.item
if NewsArray.count != 0{
if collectionView.tag == 0
{
print("cell tag = \(CollectionCell.tag)")
if NewsArray[indexPath.row].CollectionImage.elementsEqual("") && NewsArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
if NewsArray[indexPath.row].CollectionImage.elementsEqual("")
{
if NewsArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
CollectionCell.CollectionPlayButton.isHidden = false
}
}
else{
CollectionCell.CollectionPlayButton.isHidden = true
print("\(CollectionCell.tag) == \(indexPath.item) ")
if CollectionCell.tag == indexPath.item {
//CollectionCell.CollectionImageView.image = nil
if let imgUrl = URL(string: "\(ImageBaseUrl)\(NewsArray[indexPath.item].CollectionImage)")
{
CollectionCell.CollectionImageView.kf.setImage(with: imgUrl)
}else{
CollectionCell.CollectionImageView.image = nil
}
}
}
}
CollectionCell.CollectionLabel.text = NewsArray[indexPath.row].CollectionTitle
}else if collectionView.tag == 1 {
if EventsArray[indexPath.row].CollectionImage.elementsEqual("") && EventsArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
if EventsArray[indexPath.row].CollectionImage.elementsEqual("")
{
if EventsArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
CollectionCell.CollectionPlayButton.isHidden = false
}
}
else{
CollectionCell.CollectionPlayButton.isHidden = true
if let imgUrl = URL(string: "\(ImageBaseUrl)\(EventsArray[indexPath.item].CollectionImage)")
{
CollectionCell.CollectionImageView.kf.setImage(with: imgUrl)
}
}
}
CollectionCell.CollectionLabel.text = EventsArray[indexPath.row].CollectionTitle
}else if collectionView.tag == 2 {
if PostArray[indexPath.row].CollectionImage.elementsEqual("") && PostArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
if PostArray[indexPath.row].CollectionImage.elementsEqual("")
{
if PostArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
CollectionCell.CollectionPlayButton.isHidden = false
}
}
else{
CollectionCell.CollectionPlayButton.isHidden = true
if let imgUrl = URL(string: "\(ImageBaseUrl)\(PostArray[indexPath.item].CollectionImage)")
{
CollectionCell.CollectionImageView.kf.setImage(with: imgUrl)
}
}
}
CollectionCell.CollectionLabel.text = PostArray[indexPath.row].CollectionTitle
}else if collectionView.tag == 3 {
if VideosArray[indexPath.row].CollectionImage.elementsEqual("") && VideosArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
if VideosArray[indexPath.row].CollectionImage.elementsEqual("")
{
if VideosArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
CollectionCell.CollectionPlayButton.isHidden = false
}
}
else{
CollectionCell.CollectionPlayButton.isHidden = true
if let imgUrl = URL(string: "\(ImageBaseUrl)\(VideosArray[indexPath.item].CollectionImage)")
{
CollectionCell.CollectionImageView.kf.setImage(with: imgUrl)
}
}
}
CollectionCell.CollectionLabel.text = VideosArray[indexPath.row].CollectionTitle
}else if collectionView.tag == 4 {
if ResearchArray[indexPath.row].CollectionImage.elementsEqual("") && ResearchArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
if ResearchArray[indexPath.row].CollectionImage.elementsEqual("")
{
if ResearchArray[indexPath.row].CollectionVideo.elementsEqual("")
{
CollectionCell.CollectionPlayButton.isHidden = true
}else{
CollectionCell.CollectionPlayButton.isHidden = false
}
}
else{
CollectionCell.CollectionPlayButton.isHidden = true
if let imgUrl = URL(string: "\(ImageBaseUrl)\(ResearchArray[indexPath.item].CollectionImage)")
{
CollectionCell.CollectionImageView.kf.setImage(with: imgUrl)
}
}
}
CollectionCell.CollectionLabel.text = ResearchArray[indexPath.row].CollectionTitle
}
}
return CollectionCell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.MainCollectionView{
}else{
//self.performSegue(withIdentifier: "HomeToDetailHome", sender: self)
//delete below line and uncomment upper line of segue
self.performSegue(withIdentifier: "HomeToSeeAll", sender: self)
// let Tablecell = collectionView.dequeueReusableCell(withReuseIdentifier: "TabelCell", for: indexPath) as! HomeTableViewCell
let CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! HomeCollectionViewCell
print(indexPath.item)
print(collectionView.tag)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.MainCollectionView{
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
}else{
return CGSize(width: 120, height: 140)
}
}