Ios 如何在一个自定义tableView单元格中使用两个按钮?我想在单击一个按钮时更改两个按钮的图标
这是按钮操作的方法Ios 如何在一个自定义tableView单元格中使用两个按钮?我想在单击一个按钮时更改两个按钮的图标,ios,swift,uitableview,Ios,Swift,Uitableview,这是按钮操作的方法 @objc func recived() { kind = false self.viewDidLoad() self.viewWillAppear(true) } @objc func paid() { kind = true self.viewDidLoad() self.viewWillAppear(true) } 这里是t
@objc func recived()
{
kind = false
self.viewDidLoad()
self.viewWillAppear(true)
}
@objc func paid()
{
kind = true
self.viewDidLoad()
self.viewWillAppear(true)
}
这里是tableView索引XPath方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "kindCell", for: indexPath) as! KindTableViewCell
if kind == true {
cell.recivedButton.addTarget(self, action:#selector (recived), for: UIControlEvents.touchUpInside)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.normal)
cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)
}
else{
cell.paidButton.addTarget(self, action: #selector (paid), for: UIControlEvents.touchUpInside)
cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.normal)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
}
return cell
}
您可以根据状态在按钮上添加多个图像: 1:对于正常状态 1:对于所选状态 现在,您可以处理按钮状态,按钮根据按钮状态自动选择图像,您必须将其添加到自定义类中,如下所示:
override func awakeFromNib() {
super.awakeFromNib()
cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.selected)
cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)
cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.selected)
}
现在,您需要在按钮目标方法中处理按钮选择状态,如下所示:
sender.isSelected = !sender.isSelected
或者可以检查以下条件:单击了“付款”按钮,然后选择了“付款”按钮,则“付款”按钮为“真”,而“收到”按钮为“假”
例如:
@objc func recived(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
paidButton.isSelected = sender.isSelected
}
@objc func paid(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
recivedButton.isSelected = sender.isSelected
}
您应该在单元本身中处理此逻辑。在Interface Builder中连接您的插座和操作,然后
class KindTableViewCell: UITableViewCell {
@IBOutlet private weak var receivedButton: UIButton!
@IBOutlet private weak var paidButton: UIButton!
@IBAction func received(_ sender: UIButton) {
recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: .normal)
paidButton.setImage(#imageLiteral(resourceName: "red off"), for: .normal)
}
@IBAction func paid(_ sender: UIButton) {
paidButton.setImage(#imageLiteral(resourceName: "red on"), for: .normal)
recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: .normal)
}
}
请注意按钮插座是如何专用的
——这消除了在视图控制器中更新按钮插座的诱惑。让单元格更新自己的状态
如果需要在视图控制器中执行其他工作,请将代理协议添加到表视图单元格
protocol KindTableViewCellDelegate: class {
func kindTableViewCellDidTapReceive(_ kindTableViewCell: KindTableViewCell)
func kindTableViewCellDidTapPaid(_ kindTableViewCell: KindTableViewCell)
}
class KindTableViewCell: UITableViewCell {
// Outlets
var delegate: KindTableViewCellDelegate?
@IBAction func received(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapReceive(self)
}
@IBAction func paid(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapPaid(self)
}
}
然后在表视图控制器中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "kindCell", for: indexPath) as! KindTableViewCell
cell.delegate = self
return cell
}
到底什么不起作用?你不应该调用
viewDidLoad
或viewwillbeen
你自己我该怎么办?
protocol KindTableViewCellDelegate: class {
func kindTableViewCellDidTapReceive(_ kindTableViewCell: KindTableViewCell)
func kindTableViewCellDidTapPaid(_ kindTableViewCell: KindTableViewCell)
}
class KindTableViewCell: UITableViewCell {
// Outlets
var delegate: KindTableViewCellDelegate?
@IBAction func received(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapReceive(self)
}
@IBAction func paid(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapPaid(self)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "kindCell", for: indexPath) as! KindTableViewCell
cell.delegate = self
return cell
}