2017-07-31 173 views
-3

的tableview盒我是新来的斯威夫特有两个复选框[出,缺席]选中/取消做工精细编程检查与斯威夫特

enter image description here

泰伯维。

注1:进行/不存在的复选框阵列数据之后检查中的任一个追加和将数据传递到功能:

出来=>检查==>阵列数据追加==>数据传递给功能

缺席=>检查==>数据追加==>数据传递到功能

个别运作良好。注意2:如果我可以单击Out框,Out Box数据追加到数组并立即传递给我要更改Absent的复选框,然后清除Apanded out数据并将缺席数据发送到函数

出==>检查===>数据追加==> 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() 

    } 

} 
+2

有什么真正的问题? –

+0

如果我可以单击同时添加到数组的数据复选框,如果我可以单击缺席复选框。取消出箱,删除附加数据,并添加缺席复选框数据 – naga

+0

我得到了解决方案多选 – naga

回答

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 
     }