【问题标题】:Check box with tableview in SwiftSwift中带有tableview的复选框
【发布时间】:2017-07-31 19:01:40
【问题描述】:

我是 Swift 编程新手

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

注意1:在数组数据追加并将数据传递给函数后选中/不存在任何一个复选框:

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

absent =>checked==> data append ==> 数据传递给函数

个人工作良好。

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

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

如果我可以检查和取消检查两个 out/absent 函数数据,例如

身体数据状态=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)

        }

snack bar 用于执行发送动作数据传递给函数

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

    }

}

【问题讨论】:

  • 真正的问题是什么?
  • 如果我可以单击缺席复选框,则可以同时单击附加到数组的 out 复选框数据。取消选中出框并删除附加数据并添加缺席复选框数据
  • 我得到了多项选择的解决方案

标签: ios arrays swift checkbox tableview


【解决方案1】:
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
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多