【问题标题】:Selecting a custom cell activates another cell选择一个自定义单元格会激活另一个单元格
【发布时间】:2017-01-08 14:56:08
【问题描述】:

我有一个自定义单元格类,它有一个图像、一些文本标签(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 重用了单元格。我将如何实现一个功能,以确保当用户单击特定单元格的按钮时,只显示该单元格的完整描述?

表格视图代码:

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()
}

【问题讨论】:

    标签: swift tableviewcell custom-cell


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多