tableView 滚动时丢失标题 UIButton
lost title UIButton when tableView scrolling
我有一个带有多个自定义单元格的 UITableView。我现在遇到的问题是,选择按钮时必须显示标题按钮的问题,但是点键也可以更改自己的状态。样式根据状态而变化。这是积分按钮中的代码:
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")
}
}
您忘记添加:
self.tableView.reloadData()
每当您更新标题时。
维护选定索引的数组。
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)
}
}
我有一个带有多个自定义单元格的 UITableView。我现在遇到的问题是,选择按钮时必须显示标题按钮的问题,但是点键也可以更改自己的状态。样式根据状态而变化。这是积分按钮中的代码:
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")
}
}
您忘记添加:
self.tableView.reloadData()
每当您更新标题时。
维护选定索引的数组。
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)
}
}