简体   繁体   English

表格视图在单元格上滚动时创建了多个AccessoriesType

[英]table view creating multiple accessoryType on scrolling on cells

Hi i am using table view to select deselect the cell and get the desired result. 嗨,我正在使用表格视图选择取消选择单元格并获得所需的结果。 but when i check any cell and scroll down then check shows on other cells too 但是当我检查任何单元格并向下滚动然后检查其他单元格上的显示时

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
   }
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
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM