gpt4 book ai didi

swift - 选择自定义单元格会激活另一个单元格

转载 作者:行者123 更新时间:2023-11-30 13:06:52 25 4
gpt4 key购买 nike

我有一个自定义单元格类,其中包含图像、一些文本标签(1 个已截断)和一个按钮:

class CustomTVC: UITableViewCell {

/* Outlets */
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var concatenatedTitleHolder: UILabel!
@IBOutlet weak var localDateHolder: UILabel!
@IBOutlet weak var descriptionHolder: UILabel!
@IBOutlet weak var seeMoreButton: UIButton!

override func awakeFromNib() {
super.awakeFromNib()
}
}

当用户单击该按钮时,它会显示截断文本标签的完整描述。但是,我现在遇到的问题是,当用户单击特定单元格的按钮时,它会显示用户单击的单元格的完整描述,以及另一个单元格的完整描述。

我知道发生这种情况的原因是因为 TableView 通过 dequeueReusableCellWithIdentifier 重用了单元格。我将如何实现一个功能,以确保当用户单击特定单元格的按钮时,仅显示该单元格的完整描述?

表格 View 代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTVC

if listOfShows.count != 0 {
// Downloading and displaying picture
if let downloadPicture: UIImage = helperFunctions.downloadImage(listOfShows[indexPath.row].imageLink) {
cell.imageHolder.image = downloadPicture
}
// Enlarging / dismissing picture
cell.imageHolder.userInteractionEnabled = true
let newTapped = UITapGestureRecognizer(target: self, action: #selector(MainTVC.imagedTapped(_:)))
cell.imageHolder.addGestureRecognizer(newTapped)
// Concatenating channel + series + episode title
let concatenatedTitle = listOfShows[indexPath.row].channel + " " + listOfShows[indexPath.row].series + " " + listOfShows[indexPath.row].episodeTitle
// Converting into local date / time
let universalTime = helperFunctions.localDateAndTimeConverter(listOfShows[indexPath.row].originalAirDate)

/* Other labels */
cell.concatenatedTitleHolder.text = concatenatedTitle
cell.localDateHolder.text = universalTime
cell.descriptionHolder.text = listOfShows[indexPath.row].description

cell.seeMoreButton.tag = indexPath.row
cell.seeMoreButton.addTarget(self, action: #selector(MainTVC.buttonTapped(_:markedArray:)), forControlEvents: .TouchUpInside)

resetCellSettings(cell)
}
return cell
}

func buttonTapped(sender: UIButton, markedArray: [Bool]) {

let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTVC

cell.seeMoreButton.hidden = true
cell.descriptionHolder.numberOfLines = 0
cell.descriptionHolder.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.descriptionHolder.sizeToFit()
}

func resetCellSettings(cell: CustomTVC) {
cell.seeMoreButton.hidden = false
cell.descriptionHolder.numberOfLines = 1
cell.descriptionHolder.lineBreakMode = NSLineBreakMode.ByTruncatingTail
cell.descriptionHolder.sizeToFit()
}

最佳答案

您应该将buttonTapped 函数放在CustomTVC 类中。并在创建单元格时为该函数设置 seeMoreButton 的导出 IBAction。

关于swift - 选择自定义单元格会激活另一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259404/

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