lundi 31 juillet 2017

check box with tableview in swift

I am new to swift programming

enter image description here

Tableview with two check boxes [out ,Absent] check /uncheck working fine.

Note1: out/absent any one of the check box checked after array data append and pass the data to function,

out =>checked==> Array data append ==> Data pass to function

absent =>checked==> data append ==> Data pass to function

individival working well.

note 2: if I can click the Out box, out box data append to array and pass to function
immediately I want change the check box of Absent then clear the Appended out Data and send the Absent data to the function

out==>checked===>data append==>changetoAbsent==>cleartheoutdata=>>append the Absent data

pls help me .....!

this is the code

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)

    }

snack bar for perform send Action data pass to function

 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


    }

After append data pass to function

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()

    }

}




Aucun commentaire:

Enregistrer un commentaire