在Swift 3.0中,要保存TableViewCell中按钮的状态并使其持续存在,可以通过以下步骤实现:
isButtonSelected
来保存按钮的状态。class CustomTableViewCell: UITableViewCell {
var isButtonSelected: Bool = false
// 其他属性和方法
}
cellForRowAt
中,为每个TableViewCell设置按钮的状态。根据按钮的状态,设置按钮的选中状态或者非选中状态。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
// 根据按钮的状态设置按钮的选中状态或者非选中状态
cell.button.isSelected = cell.isButtonSelected
// 其他设置cell的代码
return cell
}
@IBAction func buttonTapped(_ sender: UIButton) {
guard let indexPath = tableView.indexPath(for: sender.superview?.superview as! CustomTableViewCell) else {
return
}
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
cell.isButtonSelected = !cell.isButtonSelected
tableView.reloadRows(at: [indexPath], with: .none)
}
通过以上步骤,可以实现保存TableViewCell中按钮的状态并使其持续存在。每次刷新TableView时,按钮的状态会根据保存的状态进行设置。
对于Swift 3.0 TableViewCell保存按钮状态的应用场景,可以是在一个列表中显示一组选项,用户可以通过点击按钮来选择或取消选择某个选项。例如,可以用于购物车中的商品列表,用户可以通过按钮来选择需要购买的商品。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云