首页 > 解决方案 > 如何通过多按钮选择快速获取表格视图数据?

问题描述

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

[enter image description here][1]

][2]

协议 ButtonSelect { 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 buttonPresed(_ sender: UIButton) {

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

// 像这样的例子

//[“2021 年 3 月 27 日 + 1”,“2021 年 3 月 28 日 + 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:ButtonSelect{

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")
   
    
}

}

标签: iosswift

解决方案


将星数保留在您的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


推荐阅读