当tableView滚动时丢失标题UIButton

时间:2018-01-23 09:51:33

标签: swift swift3

我有一个带有几个自定义单元格的UITableView。我现在的问题是,当选择按钮时我必须显示标题按钮但是pointButton也可以改变它自己的状态。风格根据状态而变化。这是点按钮中的代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "FrendsTableViewCellId", for: indexPath as IndexPath) as! FriendEventialTableViewCell 
        let f = self.user![indexPath.row]

        cell.userName.text = "\(f.firstname!) \(f.lastname!)"
        cell.titleName.text = f.jobTitle ?? ""

        if f.avatar != nil{
            cell.imageUser.af_setImage(withURL: URL(string: f.avatar!)!)
        }

        cell.selectButton.setTitle("Select", for: UIControlState.normal)
        cell.selectButton.setTitleColor(ColorConstant.charcoalGrey, for: UIControlState.normal)
        cell.selectButton.backgroundColor = UIColor.white
        cell.selectButton.layer.borderColor = UIColor(red: 151.0/255.0, green: 151.0/255.0, blue: 151.0/255.0, alpha: 1.0).cgColor

        cell.selectButton.addTarget(self, action: #selector(EventialFrendsViewController.followButtonClicked(sender:)), for: UIControlEvents.touchUpInside)
        cell.selectButton.tag = indexPath.row

    return cell

   } 

按钮动作,用于检测按下按钮的时间

  @objc func followButtonClicked(sender:UIButton) {
    let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
    let indexPath = self.tableView.indexPathForRow(at: buttonPosition)

        if sender.isSelected
        {
            sender.setTitle("Select", for: UIControlState.normal)
            sender.setTitleColor(ColorConstant.charcoalGrey, for: UIControlState.normal)
            sender.backgroundColor = UIColor.white
            sender.layer.borderColor = UIColor(red: 151.0/255.0, green: 151.0/255.0, blue: 151.0/255.0, alpha: 1.0).cgColor
            sender.isSelected = false
            print("select")

        }
        else
        {
            sender.setTitle("Selected", for: UIControlState.normal)
            sender.setTitleColor(ColorConstant.charcoalGrey, for: UIControlState.normal)
            sender.layer.borderColor = UIColor(red: 215.0/255.0, green: 190.0/255.0, blue: 10.0/255.0, alpha: 1.0).cgColor
            sender.backgroundColor = ColorConstant.sunYellow
            sender.isSelected = true
            print("selected")
        }

    }

2 个答案:

答案 0 :(得分:1)

维护一系列选定的索引。

var selectedIndexes: [IndexPath : Bool] = [:]

cellForRowAt方法中的选定和未选定按钮进行UI自定义。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FrendsTableViewCellId", for: indexPath as IndexPath) as! FriendEventialTableViewCell

    // Set up cell here

    if selectedIndexes[indexPath] ?? false {
        // customise cell for selected state
    } else {
        // customise cell for default state
    }
    return cell
}

在按钮回调上切换状态并重新加载该单元格。

@objc func followButtonClicked(sender:UIButton) {
    let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
    if let indexPath = self.tableView.indexPathForRow(at: buttonPosition) {
        selectedIndexes[indexPath] = !(selectedIndexes[indexPath] ?? false)
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

答案 1 :(得分:0)

你忘了添加:

self.tableView.reloadData()

每当您更新标题时。