Swift 在滚动tableView时丢失标题按钮
我有一个带有几个自定义单元格的UITableView。我现在的问题是,当按钮被选中时,我必须显示标题按钮,但pointButton也可以更改其自身的状态。样式将根据状态而更改。以下是“点”按钮中的代码:Swift 在滚动tableView时丢失标题按钮,swift,swift3,Swift,Swift3,我有一个带有几个自定义单元格的UITableView。我现在的问题是,当按钮被选中时,我必须显示标题按钮,但pointButton也可以更改其自身的状态。样式将根据状态而更改。以下是“点”按钮中的代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(w
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)
}
}
您必须在数据模型(
用户
)中保持选中状态并在cellForRow
中相应地设置标题如果更改的状态没有保存在某个地方,并且在cellForRow
中考虑,那么如果数据模型是自定义结构或类,则单独的索引数组是一个非常糟糕的主意,那么这肯定不起作用。向模型中添加适当的属性要有效得多。hi bilal i have error“不能用'Int'类型的索引为'[indexath:Bool]'类型的值下标”@bilal在selectedIndexes[indexath!]=处出错!(selectedIndexes[indexPath?.row]?false)删除“.row”。使用移动应用程序无法编译它。正如@vadian所建议的,最好在数据模型中创建一个属性以获得以下状态,而不是维护索引数组。