2016-02-26 54 views
0

嗨,我使用表視圖來選擇取消選擇單元格並獲得所需的結果。但是當我檢查的任意單元格,然後向下滾動,然後檢查顯示了太桌面視圖在滾動單元格上創建多個accessoryType

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     errorText!.removeFromSuperview() 

     tableView.deselectRowAtIndexPath(indexPath, animated: false) 
     let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 
     let person = numberArray[indexPath.row] 

     if cell.accessoryType == .None { 
      cell.accessoryType = .Checkmark 
      selectedPeople.addObject(person) 
     }else if cell.accessoryType == .Checkmark { 
      cell.accessoryType = .None 
      selectedPeople.removeObject(person) 
     } 

     print("\(selectedPeople)") 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("commentCellss", forIndexPath: indexPath) 


     for object in cell.contentView.subviews 
     { 
      object.removeFromSuperview(); 
     } 


     let text1:UILabel = UILabel.init(frame: CGRectMake(20, 10, 350, 30)) 
     text1.font = UIFont.systemFontOfSize(23) 
     text1.text = nameArray[indexPath.row] as? String 
     cell.contentView.addSubview(text1) 

     let text2:UILabel = UILabel.init(frame: CGRectMake(20, text1.frame.origin.y+text1.frame.size.height , 350, 30)) 
     text2.font = UIFont.systemFontOfSize(15) 
     text2.text = numberArray[indexPath.row] as? String 
     text2.textColor = UIColor.lightGrayColor() 
     cell.contentView.addSubview(text2) 


     let text3:UILabel = UILabel.init(frame: CGRectMake(0, text2.frame.origin.y+text2.frame.size.height+6 , 350, 1)) 
     text3.font = UIFont.systemFontOfSize(15) 
     text3.backgroundColor = UIColor.darkGrayColor() 
     cell.contentView.addSubview(text3) 



     return cell 
    } 
+0

你必須維護,因爲細胞 –

+0

的resusability的選擇,我應該怎麼做,其他細胞....意味着我必須檢查哪些單元格已經在索引路徑中的單元格中檢查行。 ? –

+0

請先給出下面的答案...... !!它爲我工作 –

回答

0
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     errorText!.removeFromSuperview() 

     tableView.deselectRowAtIndexPath(indexPath, animated: false) 
     let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 
     let person = numberArray[indexPath.row] 

     if cell.accessoryType == .None { 
      selectedPeople.addObject(person) 
     }else if cell.accessoryType == .Checkmark { 
      selectedPeople.removeObject(person) 
     } 
     tableView.reloadData() 
     print("\(selectedPeople)") 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("commentCellss", forIndexPath: indexPath) 


     for object in cell.contentView.subviews 
     { 
      object.removeFromSuperview(); 
     } 


     let text1:UILabel = UILabel.init(frame: CGRectMake(20, 10, 350, 30)) 
     text1.font = UIFont.systemFontOfSize(23) 
     text1.text = nameArray[indexPath.row] as? String 
     cell.contentView.addSubview(text1) 

     let text2:UILabel = UILabel.init(frame: CGRectMake(20, text1.frame.origin.y+text1.frame.size.height , 350, 30)) 
     text2.font = UIFont.systemFontOfSize(15) 
     text2.text = numberArray[indexPath.row] as? String 
     text2.textColor = UIColor.lightGrayColor() 
     cell.contentView.addSubview(text2) 


     let text3:UILabel = UILabel.init(frame: CGRectMake(0, text2.frame.origin.y+text2.frame.size.height+6 , 350, 1)) 
     text3.font = UIFont.systemFontOfSize(15) 
     text3.backgroundColor = UIColor.darkGrayColor() 
     cell.contentView.addSubview(text3) 

     if (selectedPeople.contains(numberArray[indexPath.row])){ 
      cell.accessoryType = .Checkmark 
     } 
     else{ 
      cell.accessoryType = .None 
     } 

     return cell 
    } 
+0

請先實現.... !!!它爲我工作......! –

+0

是的,我確定我正在實施和檢查結果.. –

+0

這是適用於你嗎? –

相關問題