以下是迄今为止已实施的内容的图片。
**目的:**以表格视图显示第三部分中年龄组的出勤总数。
我们应用的内容:已尝试应用带有新变量的标签,该变量可以计算增加和减少按钮功能的单元格。
**问题是什么:**在表视图的第三部分中,我们只希望标签显示年龄组的总出勤率,但仍会重复点击增加和减少按钮
我们的尝试:1.尝试使用新变量应用标签,该变量可以计算增加和减少按钮功能的单元格,不幸的是,如果[“0”]用于新变量以实现总和,则应用程序崩溃。--〉var itemValue 2 = [“0”] 2.尝试使用新控制器实现,但发生了同样的事情。
下面是类AdditionalGuestInformationVC:的代码:
import UIKit
import ObjectiveC
import SSSpinnerButton
class AdditionalGuestInforTVCell: UITableViewCell {
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblDetails: UILabel!
@IBOutlet var btnMinus: UIButton!
@IBOutlet var lblValue: UILabel!
@IBOutlet var btnPlus: UIButton!
}
class AdditionalGuestInformationVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let section = ["Male", "Female", "total"]
let itemsName = [["Shishu", "Baal", "Kishore", "Tarun", "Yuva", "Jyeshta"], ["Shishu", "Baalika", "Kishori", "Taruni", "Yuvati", "Jyeshtaa"], ["Total:"]]
let itemsDetails = [["Below 5 Years - Pre-primary ", "5 to 11 Years - Primary School", "11 to 16 Years - Middle School", "17 to 25 Years - High School/College", "25 to 60 Years - Adults", ">60 Years - Senior citizen"], ["Below 5 Years - Pre-primary ", "5 to 11 Years - Primary School", "11 to 16 Years - Middle School", "17 to 25 Years - High School/College", "25 to 60 Years - Adults", ">60 Years - Senior citizen"],["Attendees"]]
var itemValue = [["0", "0", "0", "0", "0", "0"], ["0", "0", "0", "0", "0", "0"], ["0"]]
var dicMember = [[String:Any]]()
var strDate = ""
var utsavName = ""
@IBOutlet weak var tableView: UITableView!
@IBOutlet var lblShakhaName: UILabel!
@IBOutlet var lblDate: UILabel!
@IBOutlet var btnSubmit: SSSpinnerButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.fillData()
}
override func viewWillAppear(_ animated: Bool) {
navigationBarDesign(txt_title: "guest_information".localized, showbtn: "back")
self.firebaseAnalytics(_eventName: "AdditionalGuestInfoVC")
}
func fillData() {
self.lblDate.text = strDate
}
@objc func onMinusClick(_ sender: UIButton) {
let sectionIndex = ((sender.tag / 10) - 10)
let rowIndex = (sender.tag % 10)
let indexPath = NSIndexPath(row: rowIndex, section: sectionIndex)
let cell = tableView.cellForRow(at: indexPath as IndexPath) as? AdditionalGuestInforTVCell
var strVal : Int = Int((cell?.lblValue.text)!)!
if strVal > 0 {
strVal -= 1
}
self.itemValue[sectionIndex][rowIndex] = "\(Int(strVal))"
tableView.reloadRows(at:[indexPath as IndexPath], with:.automatic)
}
@objc func onPlusClick(_ sender: UIButton) {
let sectionIndex = ((sender.tag / 10) - 10)
let rowIndex = (sender.tag % 10)
let indexPath = NSIndexPath(row: rowIndex, section: sectionIndex)
let cell = tableView.cellForRow(at: indexPath as IndexPath) as? AdditionalGuestInforTVCell
var strVal : Int = Int((cell?.lblValue.text)!)!
if strVal < 99 {
strVal += 1
}
self.itemValue[sectionIndex][rowIndex] = "\(Int(strVal))"
tableView.reloadRows(at:[indexPath as IndexPath], with:.automatic)
}
@IBAction func onPreviewClick(_ sender: UIButton) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "AttendancePreviewVC") as! AttendancePreviewVC
vc.itemsName = self.itemsName
vc.itemsDetails = self.itemsDetails
vc.itemValue = self.itemValue
vc.dicMember = self.dicMember
self.navigationController?.pushViewController(vc, animated: true)
}
@IBAction func onSubmitClick(_ sender: UIButton) {
btnSubmit.startAnimate(spinnerType: SpinnerType.circleStrokeSpin, spinnercolor: .white, spinnerSize: 20, complete: {
// Your code here
self.addSankhyaAPI()
let disableMyButton = sender as? UIButton
disableMyButton?.isEnabled = false
})
}
// MARK: TableView Methods
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return self.section[section]
}
func numberOfSections(in tableView: UITableView) -> Int
{
// #warning Incomplete implementation, return the number of sections
return self.section.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rect = CGRect(x: 20, y: 0, width: tableView.frame.size.width - 40, height: 60)
let footerView = UIView(frame:rect)
footerView.backgroundColor = UIColor.init(red: 229.0/255.0, green: 229.0/255.0, blue: 229.0/255.0, alpha: 1.0)
let lblTitle = UILabel(frame: CGRect(x: 20.0, y: footerView.center.y / 2, width: 160, height: 30))
lblTitle.text = "Guest Information"
lblTitle.font = UIFont(name: "SourceSansPro-Regular", size: 20.0)
lblTitle.backgroundColor = UIColor.clear
lblTitle.textColor = UIColor.init(red: 154.0/255.0, green: 154.0/255.0, blue: 154.0/255.0, alpha: 1.0)
let lblGender = UILabel(frame: CGRect(x: 30.0 + lblTitle.frame.width, y: footerView.center.y / 2, width: 80, height: 30))
lblGender.text = self.section[section]
lblGender.font = UIFont(name: "SourceSansPro-SemiBold", size: 16.0)
lblGender.textColor = UIColor.black
lblGender.backgroundColor = UIColor.init(red: 255.0/255.0, green: 230.0/255.0, blue: 190.0/255.0, alpha: 1.0)
lblGender.layer.cornerRadius = 15.0
lblGender.layer.masksToBounds = true
lblGender.textAlignment = .center
footerView.addSubview(lblGender)
footerView.addSubview(lblTitle)
return footerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60 // or whatever
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemsName[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AdditionalGuestInforTVCell", for: indexPath) as! AdditionalGuestInforTVCell
cell.lblName.text = self.itemsName[indexPath.section][indexPath.row]
cell.lblDetails.text = self.itemsDetails[indexPath.section][indexPath.row]
cell.lblValue.text = self.itemValue[indexPath.section][indexPath.row]
cell.btnMinus.addTarget(self, action: #selector(onMinusClick(_:)), for: .touchUpInside)
cell.btnPlus.addTarget(self, action: #selector(onPlusClick(_:)), for: .touchUpInside)
let strSection : String = "\(indexPath.section + 10)"
let strRow = "\(indexPath.row)"
cell.btnPlus.tag = Int(strSection + strRow)!
cell.btnMinus.tag = Int(strSection + strRow)!
cell.lblValue.tag = Int(strSection + strRow)!
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension //Choose your custom row height
}
func addSankhyaAPI() {
let arrFilter : [String] = self.dicMember.filter { $0["isPresent"] as! String == "true" }.map { $0["member_id"]! as! String }
let stringArr = arrFilter.joined(separator: " ")
let stringArray = stringArr.replacingOccurrences(of: " ", with: ", ", options: .literal, range: nil)
var parameters: [String: Any] = [:]
parameters["user_id"] = _appDelegator.dicMemberProfile![0]["user_id"] as? String // dicUserDetails["user_id"]
parameters["event_date"] = strDate
parameters["org_chapter_id"] = _appDelegator.dicMemberProfile![0]["shakha_id"]
parameters["utsav"] = utsavName
parameters["member_id"] = stringArray
parameters["shishu_male"] = self.itemValue[0][0]
parameters["baal"] = self.itemValue[0][1]
parameters["kishore"] = self.itemValue[0][2]
parameters["tarun"] = self.itemValue[0][3]
parameters["yuva"] = self.itemValue[0][4]
parameters["proudh"] = self.itemValue[0][5]
parameters["shishu_female"] = self.itemValue[1][0]
parameters["baalika"] = self.itemValue[1][1]
parameters["kishori"] = self.itemValue[1][2]
parameters["taruni"] = self.itemValue[1][3]
parameters["yuvati"] = self.itemValue[1][4]
parameters["proudha"] = self.itemValue[1][5]
parameters["total:"] = self.itemValue[2]
parameters["api"] = "yes"
print(parameters)
APIManager.sharedInstance.callPostApi(url: APIUrl.add_sankhya, parameters: parameters) { (jsonData, error) in
if error == nil
{
if let status = jsonData!["status"].int
{
if status == 1
{
self.btnSubmit.stopAnimationWithCompletionTypeAndBackToDefaults(completionType: .success, backToDefaults: true, complete: {
// Your code here
if let strMessage = jsonData!["message"].string {
showAlert(title: APP.title, message: strMessage)
}
})
}else {
self.btnSubmit.stopAnimationWithCompletionTypeAndBackToDefaults(completionType: .fail, backToDefaults: true, complete: {
// Your code here
if let strError = jsonData!["message"].string {
showAlert(title: APP.title, message: strError)
}
})
}
} else {
self.btnSubmit.stopAnimationWithCompletionTypeAndBackToDefaults(completionType: .fail, backToDefaults: true, complete: {
// Your code here
if let strError = jsonData!["message"].string {
showAlert(title: APP.title, message: strError)
}
})
}
}
}
}
}
1条答案
按热度按时间w8f9ii691#
您希望将按钮操作移动到
AdditionalGuestInforTVCell
cell类内部,然后使用closure
通知控制器值发生了更改。使用第二个
UITableViewCell
类作为"Total"单元格-带有两个标签,因此它看起来如下所示:当你的
AdditionalGuestInforTVCell
单元告诉控制器它的值改变了,更新你的数据,得到所有"项"的总和,并重新加载第三部分。这里有一个简单的例子...
首先,让我们使用
struct
来定义数据:在控制器中,我们将使用如下的var属性:
我们将这样初始化数据。
在
viewDidLoad()
中:下面是一个完整的、可运行的示例,我们通过代码创建所有内容--没有
@IBOutlet
或@IBAction
连接--因此您所需要做的就是将普通UIViewController
的自定义类赋值为AdditionalGuestInformationVC
,我尝试在代码中包含足够多的行内注解以使事情更清楚:在运行时应如下所示:
第一节第一节第一节第二节第一节