在Swift中使用tableview复选框

时间:2017-07-31 19:01:40

标签: ios arrays swift checkbox tableview

我是Swift编程新手

enter image description here

Tableview有两个复选框[out,Absent]检查/取消选中正常工作。

注1:在数组数据追加后检查复选框中的任何一个是否缺失并将数据传递给函数:

  

out => checked ==>数组数据追加==>数据传递给函数

     

缺席=>已检查==> data append ==>数据传递给函数

个人运作良好。

注意2:如果我可以单击Out框,out box数据附加到数组并立即传递给函数我想更改Absent复选框然后清除Appended out Data并将Absent数据发送到函数

  

out ==> checked ===> data append ==> changetoAbsent ==> cleartheoutdata =>>追加缺席数据

如果我可以检查并取消选中

之类的输出/缺席功能数据
  

正文数据状态= 2& staffId = 18& studentId = 5& attendanceId = 6677

     

正文数据状态= 3& staffId = 18& studentId = 5& attendanceId = 6677

     

正文数据状态= 2& staffId = 18& studentId = 5& attendanceId = 6677

我选中该复选框数据只传递给函数

这是代码:

 var InCheckec = [Bool]()

    var OutCheckec = [Bool]()

    var AbsentCheckec = [Bool]()

    var upStudentId = [String]()

    var upAttendanceID = [String]()

    var upStatus : String = ""

    var upStaffId : String  = ""

    var UPstatusTest = [String]()

    var inButtoncount : Int = 0

  @IBAction func InButttonClick(_ sender: UIButton) {

            status = "1"

            snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
                ,green: CGFloat(0xB1)/255
                ,blue: CGFloat(0xB1)/255
                ,alpha: 1.0)

            let prefs:UserDefaults = UserDefaults.standard
            StaffID = prefs.value(forKey: "STAFFID") as! String


            let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
            let indexPath = self.TableView.indexPathForRow(at: position)
            let _: UITableViewCell = TableView.cellForRow(at: indexPath!)!

            _ = (indexPath! as NSIndexPath).row

            let buttontag = sender.tag

            if (sender.isSelected == true)
            {
                InCheckec[buttontag] = false

                inButtoncount -= 1

                if(inButtoncount == 0)
                {
                    snackbar.dismiss()
                    upStaffId.removeAll()
                    upStudentId.removeAll()
                    upAttendanceID.removeAll()

                }
                else{

                    upStudentId.removeLast()
                    upAttendanceID.removeLast()
                    UPstatusTest.removeLast()

                }
            }

            else
            {

                snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
                    ,green: CGFloat(0xB1)/255
                    ,blue: CGFloat(0xB1)/255
                    ,alpha: 1.0)

                InCheckec[buttontag] = true

                inButtoncount += 1
                currentSnackbar = snackbar
                snackbar.show()


                let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo


                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID

            }

            self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

        }

        @IBAction func OUTBUTTON(_ sender: UIButton) {
            snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
                ,green: CGFloat(0xB1)/255
                ,blue: CGFloat(0xB1)/255
                ,alpha: 1.0)


            status = "2"

            let buttontag = sender.tag

            let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
            let indexPath = self.TableView.indexPathForRow(at: position)



            if (sender.isSelected == true)

            {

                OutCheckec[buttontag] = false
                inButtoncount -= 1


                if(inButtoncount == 0)
                {
                    snackbar.dismiss()
                    upStudentId.removeAll()
                    upAttendanceID.removeAll()
                    UPstatusTest.removeAll()
                }
                else{

                    upStudentId.removeLast()
                    UPstatusTest.removeLast()
                    upAttendanceID.removeLast()

                }

            }
            else
            {


                OutCheckec[buttontag] = true
                AbsentCheckec[buttontag] = false
                print("buttontab value",buttontag)


                inButtoncount += 1
                snackbar.show()

                let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo

                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID

            }



            self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

        }


        @IBAction func ABSENTBUTTON(_ sender: UIButton) {

            status = "3"
            snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
                ,green: CGFloat(0xB1)/255
                ,blue: CGFloat(0xB1)/255
                ,alpha: 1.0)

            let buttontag = sender.tag
            let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
            let indexPath = self.TableView.indexPathForRow(at: position)



            if (sender.isSelected == true)

            {

                AbsentCheckec[buttontag] = false

                inButtoncount -= 1


                if(inButtoncount == 0)
                {
                    snackbar.dismiss()
                    upStudentId.removeAll()
                    upAttendanceID.removeAll()
                    UPstatusTest.removeAll()
                }
                else{

                    upStudentId.removeLast()
                    UPstatusTest.removeLast()
                    upAttendanceID.removeLast()

                }


            }
            else
            {
                AbsentCheckec[buttontag] = true

                OutCheckec[buttontag] = false

                inButtoncount += 1
                snackbar.show()

                let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo


                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID



            }
            self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

        }

快餐栏执行发送动作数据传递给功能

 lazy var snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND") { (snackbar) in

        self.activityIndicatorView.startAnimating()

        self.updateArray(_upstatus:UPstatusTest,UPStudentId:upStudentId,UPAttendanceID:upAttendanceID,UPStaffId:upStaffId,inBcount:inButtoncount)

        UPstatusTest.removeAll()
        upStudentId.removeAll()
        upAttendanceID.removeAll()
        inButtoncount = 0


    }

将数据传递给函数后

func updateArray(_upstatus:[String],UPStudentId:[String],UPAttendanceID:[String],UPStaffId:String,inBcount:Int)
{

    for i in 0..<inBcount

    {

        var errorCode = "1"

        _ = "Failed"



        var request = URLRequest(url: URL(string: "updateattendance",
                                          relativeTo: URL(string: serverURL+"/rkapi/api/"))!)

        let session = URLSession.shared


        request.httpMethod = "POST"

        let bodyData = "status=\(_upstatus[i])&staffId=\(UPStaffId)&studentId=\(UPStudentId[i])&attendanceId=\(upAttendanceID[i])"

        print("body data \(bodyData)")

        request.httpBody = bodyData.data(using: String.Encoding.utf8);
        let task = session.dataTask(with: request, completionHandler: { (data, response, error) in

            do {

                if data != nil {

                    if let jsonData = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {


                        errorCode = String(describing: jsonData["errorCode"]!)


                        if(errorCode == "0")
                        {

                            self.getAttendances()

                        }


                    }


                }

                else {

                    self.displayAlert("Raksha Data", message: "Data Not Available. Please try again")

                }

            } catch _ as NSError {

            }

        })

        task.resume()

    }

}

1 个答案:

答案 0 :(得分:0)

this is the solution for checkboxes with multiple selection 

     @IBAction func InButttonClick(_ sender: UIButton) {

            status = "1"

            snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
                ,green: CGFloat(0xB1)/255
                ,blue: CGFloat(0xB1)/255
                ,alpha: 1.0)

            let prefs:UserDefaults = UserDefaults.standard
            StaffID = prefs.value(forKey: "STAFFID") as! String

            let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
            let indexPath = self.TableView.indexPathForRow(at: position)
            let _: UITableViewCell = TableView.cellForRow(at: indexPath!)!

            _ = (indexPath! as NSIndexPath).row

            let buttontag = sender.tag

            if (sender.isSelected == true)
            {
                InCheckec[buttontag] = false

                inButtoncount -= 1

                if(inButtoncount == 0)
                {
                    snackbar.dismiss()
                    upStaffId.removeAll()
                    upStudentId.removeAll()
                    upAttendanceID.removeAll()

                }
                else{

                    upStudentId.removeLast()
                    upAttendanceID.removeLast()
                    UPstatusTest.removeLast()

                }
            }

            else
            {

                InCheckec[buttontag] = true
                inButtoncount += 1
                currentSnackbar = snackbar
                snackbar.show()


                 let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo
            }

                var checkOldData: Bool=false
            if(upStudentId.count>0)
            {
                for (index, element) in upStudentId.enumerated() {
                    if (element==kid.studentId) {
                        checkOldData=true
                        UPstatusTest[index]="1"
                    }else{
                        print("not there")
                    }
                }

                if checkOldData==false {
                    upStudentId.append(kid.studentId!)
                    upAttendanceID.append(kid.attendanceId)
                    UPstatusTest.append(status)
                    upStaffId = StaffID
                }
            }else{
                print("new value ")
                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID
            }
        }

                self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

        }




@IBAction func OUTBUTTON(_ sender: UIButton) {


    snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
        ,green: CGFloat(0xB1)/255
        ,blue: CGFloat(0xB1)/255
        ,alpha: 1.0)


    status = "2"

    let buttontag = sender.tag

    let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
    let indexPath = self.TableView.indexPathForRow(at: position)

    let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo
    if (sender.isSelected == true)

    {

        OutCheckec[buttontag] = false
        inButtoncount -= 1
        if(inButtoncount == 0)
        {
            snackbar.dismiss()
            upStudentId.removeAll()
            upAttendanceID.removeAll()
            UPstatusTest.removeAll()
        }
        else{

            upStudentId.removeLast()
            UPstatusTest.removeLast()
            upAttendanceID.removeLast()

        }

    }
    else
    {

        OutCheckec[buttontag] = true
        AbsentCheckec[buttontag] = false
        inButtoncount += 1
        snackbar.show()

        var checkOldData: Bool=false
        if(upStudentId.count>0)
        {
            for (index, element) in upStudentId.enumerated() {
                if (element==kid.studentId) {
                    print("already there")
                    checkOldData=true
                    UPstatusTest[index]="2"
                }else{
                    print("not there")
                }
            }

            if checkOldData==false {
                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID
            }
        }else{
             print("new value ")
            upStudentId.append(kid.studentId!)
            upAttendanceID.append(kid.attendanceId)
            UPstatusTest.append(status)
            upStaffId = StaffID
        }
    }

    self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

}



@IBAction func ABSENTBUTTON(_ sender: UIButton) {

    status = "3"
    snackbar.backgroundColor = UIColor(red: CGFloat(0x00)/255
        ,green: CGFloat(0xB1)/255
        ,blue: CGFloat(0xB1)/255
        ,alpha: 1.0)

    let buttontag = sender.tag
    let position: CGPoint = sender.convert(CGPoint(x: 10, y: 20), to: self.TableView)
    let indexPath = self.TableView.indexPathForRow(at: position)
    let kid = attendanceInfo[(indexPath?.row)!] as AttendanceInfo

    if (sender.isSelected == true)

    {
        AbsentCheckec[buttontag] = false
        inButtoncount -= 1
        if(inButtoncount == 0)
        {
            snackbar.dismiss()
            upStudentId.removeAll()
            upAttendanceID.removeAll()
            UPstatusTest.removeAll()
        }
        else{

            upStudentId.removeLast()
            UPstatusTest.removeLast()
            upAttendanceID.removeLast()

        }
    }
    else
    {
        AbsentCheckec[buttontag] = true

        OutCheckec[buttontag] = false


        inButtoncount += 1
        snackbar.show()


        var checkOldData: Bool=false
        if(upStudentId.count>0)
        {
            for (index, element) in upStudentId.enumerated() {
                if (element==kid.studentId) {
                    print("already there")
                    checkOldData=true
                    UPstatusTest[index]="3"
                }else{
                    print("not there")
                }
            }

            if checkOldData==false {
                upStudentId.append(kid.studentId!)
                upAttendanceID.append(kid.attendanceId)
                UPstatusTest.append(status)
                upStaffId = StaffID
            }
        }else{
            print("new value ")
            upStudentId.append(kid.studentId!)
            upAttendanceID.append(kid.attendanceId)
            UPstatusTest.append(status)
            upStaffId = StaffID
        }
    }

    self.TableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)

}

        lazy var snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND") { (snackbar) in

            self.activityIndicatorView.startAnimating()
    self.updateArray(_upstatus:UPstatusTest,UPStudentId:upStudentId,UPAttendanceID:upAttendanceID,UPStaffId:upStaffId,inBcount:inButtoncount)
            UPstatusTest.removeAll()
            upStudentId.removeAll()
            upAttendanceID.removeAll()
            inButtoncount = 0
        }