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

时间:2016-08-31 21:37:09

标签: swift tableviewcell custom-cell

我有一个自定义单元格类,它有一个图像,一些文本标签(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()
}

1 个答案:

答案 0 :(得分:0)

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