2017-07-29 97 views
1

enter image description here的tableview數據不能正常顯示迅速

泰伯維三個複選框[IN,OUT,無]如果單擊任意一個複選框[IN,OUT,缺席]這是調用JSON的服務 並顯示數據除[Out]複選框外,所有桌面視圖都可以正常工作。如果[Out]複選框的結果在[outlabel:11:54 PM]之類的標籤上必須顯示。它在[outlabelg:]處顯示爲空 ,如果我在tableview中單擊下一個單元格[out checkbox],更新了privieous [out:Labledata]數據並顯示選定單元格[out:lable]數據。請幫我在哪裏,我沒有錯

這是實現代碼如下代碼

func numberOfSections(in tableView: UITableView) -> Int { 

    return (attendanceInfo.count == 0) ? 0 : 1 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return attendanceInfo.count 

} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = 
     tableView.dequeueReusableCell(
      withIdentifier: "DetailsCell", for: indexPath) as! AttendanceDetailsTableViewCell 

    let row = (indexPath as NSIndexPath).row 


    cell.InButton.tag = row 

    cell.OutButton.tag = row 

    cell.AbsentButton.tag = row 

    let maskLayer = CAShapeLayer() 

    let bounds = cell.bounds 

    maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 5, y: 3, width: bounds.width-15, height: bounds.height-15), cornerRadius: 2).cgPath 
    cell.layer.mask = maskLayer 


    if(attendanceInfo.count>0){ 

     let attendance = attendanceInfo[row] as AttendanceInfo 

     cell.AttendnceLabel.textColor = UIColor(red: CGFloat(0x00)/255 
      ,green: CGFloat(0xB1)/255 
      ,blue: CGFloat(0xB1)/255 
      ,alpha: 1.0) 

     cell.AbsentInfoLabel.isHidden = true 

     cell.Absentinfovalue.isHidden = true 


     if(attendance.inTime == "")&&(attendance.outTime == "") 
     { 


      cell.InButton.isHidden = false 
      cell.OutButton.isHidden = true 
      cell.AbsentButton.isHidden = true 
      cell.OutinfoLabel.isHidden = false 


     } 

     if(attendance.inTime != "") 
     { 


      cell.InButton.isHidden = true 
      cell.OutButton.isHidden = false 
      cell.AbsentButton.isHidden = false 
      cell.OutinfoLabel.isHidden = false 

     } 

     if(attendance.inTime != "") && (attendance.outTime != "") 
     { 

      cell.InButton.isHidden = true 
      cell.OutButton.isHidden = true 
      cell.AbsentButton.isHidden = true 
      cell.OutinfoLabel.isHidden = false 
      cell.OutLable.text = attendance.outTime 

     } 

     if(attendance.attendance == "PRESENT") 

     { 
      cell.OutLable.text = attendance.outTime 



     } 

     if(attendance.attendance == "ABSENT") 
     { 
      cell.InButton.isHidden = true 
      cell.OutButton.isHidden = true 
      cell.AbsentButton.isHidden = true 

      cell.Absentinfovalue.isHidden = false 
      cell.OutLable.isHidden = true 
      cell.OutinfoLabel.isHidden = true 

      cell.AttendnceLabel.textColor = UIColor.red 
      cell.Absentinfovalue.text = attendance.updatedTime 


     } 

     cell.Class.text = attendance.standard 

     cell.NameLabel.text = attendance.name 

     cell.InLabel.text = attendance.inTime 

     cell.OutLable.text = attendance.outTime 

     cell.AttendnceLabel.text = attendance.attendance 

     cell.DateLablel.text = attendance.attendanceDate 

     cell.attendanceid = attendance.attendanceId 


     cell.backgroundView = UIImageView(image: UIImage(named: "background_common")!) 


     cell.Profileimage.image = UIImage(named: "profile_pic") 
     cell.Profileimage.downloadImageFrom(link: attendance.photo!, contentMode: UIViewContentMode.scaleAspectFit) 


     if(InCheckec[indexPath.row]==true) 
     { 
      cell.InButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
      cell.InButton.isSelected = true 

     } 
     else{ 


      cell.InButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
      cell.InButton.isSelected = false 
     } 

     if(OutCheckec[indexPath.row]==true) 
     { 
      cell.OutButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
      cell.OutButton.isSelected = true 

     } 
     else{ 


      cell.OutButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
      cell.OutButton.isSelected = false 
     } 



     if(AbsentCheckec[indexPath.row]==true) 
     { 
      cell.AbsentButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
      cell.AbsentButton.isSelected = true 

     } 
     else{ 


      cell.AbsentButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
      cell.AbsentButton.isSelected = false 
     } 
    } 

    return cell 

} 

這是出於複選框的代碼[中,不存在也同樣喜歡的代碼,這種方式]

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



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

     let out:Bool = true 

     prefs.set(out, forKey:"1") 


     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) 

} 

這是的Json結果 enter image description here

+0

不相關,但'if(attendanceInfo.count> 0){'完全沒用。如果數組爲空,那麼'numberOfRows'返回0,'cellForRow'根本不被調用。 – vadian

+0

Json Data存儲在考勤信息中,如何計算學生 – naga

+0

我的意思是這個檢查沒有意義,它永遠不會評估爲假。 – vadian

回答

0

這是工作的代碼

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = 
      tableView.dequeueReusableCell(
       withIdentifier: "DetailsCell", for: indexPath) as! AttendanceDetailsTableViewCell 

     let row = (indexPath as NSIndexPath).row 


     cell.InButton.tag = row 

     cell.OutButton.tag = row 

     cell.AbsentButton.tag = row 

     let maskLayer = CAShapeLayer() 

     let bounds = cell.bounds 

     maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 5, y: 3, width: bounds.width-15, height: bounds.height-15), cornerRadius: 2).cgPath 
     cell.layer.mask = maskLayer 


     if(attendanceInfo.count>0){ 

     let attendance = attendanceInfo[row] as AttendanceInfo 

      cell.AttendnceLabel.textColor = UIColor(red: CGFloat(0x00)/255 
       ,green: CGFloat(0xB1)/255 
       ,blue: CGFloat(0xB1)/255 
       ,alpha: 1.0) 


     if(attendance.inTime == "")&&(attendance.outTime == "") 
      { 


       cell.InButton.isHidden = false 
       cell.OutButton.isHidden = true 
       cell.AbsentButton.isHidden = true 
      } 

      if(attendance.inTime != "") 
      { 
       cell.InButton.isHidden = true 
       cell.OutButton.isHidden = false 
       cell.AbsentButton.isHidden = false 
      } 


      if(attendance.inTime != "") && (attendance.outTime != "") 
      { 

       cell.InButton.isHidden = true 
       cell.OutButton.isHidden = true 
       cell.AbsentButton.isHidden = true 
       cell.OutLable.text = attendance.outTime 

      } 
      cell.OutLable.text=" " 
      cell.OutinfoLabel.text="Out" 

      if(attendance.attendance == "PRESENT") 

      { 
       cell.OutLable.text = attendance.outTime 
       cell.OutinfoLabel.text = "Out" 

      }else if(attendance.attendance == "ABSENT") 
      { 
       cell.InButton.isHidden = true 
       cell.OutButton.isHidden = true 
       cell.AbsentButton.isHidden = true 

       cell.OutinfoLabel.text = "Absent11" 
       cell.OutLable.text = attendance.updatedTime 
       cell.AttendnceLabel.textColor = UIColor.red 

      } 


      cell.Class.text = attendance.standard 

      cell.NameLabel.text = attendance.name 

      cell.InLabel.text = attendance.inTime 

      cell.AttendnceLabel.text = attendance.attendance 

      cell.DateLablel.text = attendance.attendanceDate 

      cell.attendanceid = attendance.attendanceId 


      cell.backgroundView = UIImageView(image: UIImage(named: "background_common")!) 


      cell.Profileimage.image = UIImage(named: "profile_pic") 
      cell.Profileimage.downloadImageFrom(link: attendance.photo!, contentMode: UIViewContentMode.scaleAspectFit) 


      if(InCheckec[indexPath.row]==true) 
      { 
       cell.InButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
       cell.InButton.isSelected = true 

      } 
      else{ 


       cell.InButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
       cell.InButton.isSelected = false 
      } 

      if(OutCheckec[indexPath.row]==true) 
      { 
       cell.OutButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
       cell.OutButton.isSelected = true 

      } 
      else{ 


       cell.OutButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
       cell.OutButton.isSelected = false 
      } 



      if(AbsentCheckec[indexPath.row]==true) 
      { 
       cell.AbsentButton.setBackgroundImage(UIImage(named: "Check_Box"), for: UIControlState.normal) 
       cell.AbsentButton.isSelected = true 

      } 
      else{ 


       cell.AbsentButton.setBackgroundImage(UIImage(named: "uncheck_Box"), for: UIControlState.normal) 
       cell.AbsentButton.isSelected = false 
      } 
     } 

     return cell 

    }