提问者:小点点

如何使用多按钮选择快速获取tableview数据?


我想在点击save按钮后从tableview获取数据。但是当用户第二次更改值时,相同的值两次追加。[

[在此处输入图像说明][1]

][2]

协议按钮选择{func tapStar(IndexPath:IndexPath,star:Int)}

类LeavedayDetailScell:UITableViewCell{

@IBOutlet weak var leaveDateLabel: UILabel!
@IBOutlet weak var leaveDateName: UILabel!
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var seconButton: UIButton!
@IBOutlet weak var fullButton: UIButton!

var delegate:ButtonSelect?
var indexpath:IndexPath?




   @IBAction func bittonPressed(_ sender: UIButton) {
    
    delegate?.tapStar(indexPath: indexpath!, star: sender.tag)
}

}

类DayDetailsController:UIViewController

@IBAction func ButtonPressed(_sender:UIButton){

   // i want to submit tableview result with radio button value change this place 

//示例如

//[“03/27/2021+1”,“03/28/2021+2”]

//我的结果

//[“03/27/2021+1”,“03/28/2021+2”,“03/27/2021+3”,“03/28/2021+2”]//相同值两次

    }

}

扩展DayDetailsController:UITableViewDelegate,UITableViewDataSource{func tableView(_tableView:UITableView,numberOfRowsInSection section:Int)->Int{

    return listDayDetailsData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"LeaveDayDetailsCell",for: indexPath) as! LeaveDayDetailsCell
    
    cell.leaveDateLabel.text = listDayDetailsData[indexPath.row]["BreakDate"] as? String ?? "-"
    cell.leaveDateName.text = listDayDetailsData[indexPath.row]["DateName"] as? String ?? "-"
    
    
    
    
    cell.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    cell.layer.borderWidth = 2
    dayDetailsTableView.rowHeight = 132.00
    cell.delegate = self
    cell.indexpath = indexPath
    return cell
    
}

}

扩展DayDetailsController:按钮选择{

func tapStar(indexPath: IndexPath, star: Int) {
    
    let cell = dayDetailsTableView.cellForRow(at: indexPath) as! LeaveDayDetailsCell
    
    if star == 1{
        
        
        cell.firstButton.setImage(UIImage(named: "radioFull.png"), for: .normal)
        cell.seconButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        cell.fullButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        
        
        totalvalue = star
        
    }
    
    else if star == 2{
        
        cell.firstButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        cell.seconButton.setImage(UIImage(named: "radioFull.png"), for: .normal)
        cell.fullButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        
        
        totalvalue = star
        
        
        
    }
    
    else  if star == 3{
        
        
        
        cell.firstButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        cell.seconButton.setImage(UIImage(named: "radioBlank.png"), for: .normal)
        cell.fullButton.setImage(UIImage(named: "radioFull.png"), for: .normal)
        
        
        totalvalue = star
        
        
    }
    
   
    
  
    

    answerValue.append("\(listDayDetailsData[indexPath.row]["BreakDate"] as! String) + \(totalvalue)")
    
    print(answerValue,"****answer")
   
    
}

}


共1个答案

匿名用户

在listDayDetailsData中保留星计数,并根据按钮点击更新值

离开

class LeaveDayDetailsCell: UITableViewCell {

@IBOutlet weak var leaveDateLabel: UILabel!
@IBOutlet weak var leaveDateName: UILabel!
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var seconButton: UIButton!
@IBOutlet weak var fullButton: UIButton!

}

DayDetailsController扩展

extension DayDetailsController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return listDayDetailsData.count
}

@objc func firstButtonPressed(sender: UIButton) {
    
    let index = sender.tag
    // Update star value in listDayDetailsData[index]
    
    // Reload single row at index 0
    dayDetailsTableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
}
@objc func secondButtonPressed(sender: UIButton) {
    
    let index = sender.tag
    // Update star value in listDayDetailsData[index]
    
    // Reload single row at index 0
    dayDetailsTableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
}
@objc func fullButtonPressed(sender: UIButton) {
    
    let index = sender.tag
    // Update star value in listDayDetailsData[index]
    
    // Reload single row at index 0
    dayDetailsTableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"LeaveDayDetailsCell",for: indexPath) as! LeaveDayDetailsCell
    
//    cell.leaveDateLabel.text = listDayDetailsData[indexPath.row]["BreakDate"] as? String ?? "-"
//    cell.leaveDateName.text = listDayDetailsData[indexPath.row]["DateName"] as? String ?? "-"
    
    cell.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    cell.layer.borderWidth = 2
    cell.firstButton.tag = indexPath.row
    cell.seconButton.tag = indexPath.row
    cell.fullButton.tag = indexPath.row
    
    cell.firstButton.addTarget(self, action: #selector(firstButtonPressed(sender:)), for: .touchUpInside)
    cell.firstButton.addTarget(self, action: #selector(secondButtonPressed(sender:)), for: .touchUpInside)
    cell.firstButton.addTarget(self, action: #selector(fullButtonPressed(sender:)), for: .touchUpInside)
    
    switch listDayDetailsData[indexPath.row]["Star"] as! Int {
    case 1:
        // Update your cell buttons
        break
    case 2:
        // Update your cell buttons
        break
    case 3:
        // Update your cell buttons
        break
    default:
        // Default case
        break
    }
    return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 132.00
}
}

专业提示:

使用对象来保存数据比使用字典更好

例如

struct DayDetails {

var breakDate: String

var dateName: String

var stars: Int

//....

}

还有第三方库可用于处理星级UI以及https://github.com/evgenyneu/cosmos