当accessoryType更改为Checkmark时,单元格内容重新排序

时间:2015-01-14 11:28:44

标签: ios uitableview swift

当连续激活复选标记时,iOS 7.1中的TableViewCell高度存在问题。当勾选标记打开时,即使我在Interface Builder中调整文本标签的大小,单元格的文本也会重新排序,有时会从底部单元格边距中删除(参见图像)。

勾选: enter image description here

选中标记: enter image description here

这是我的细胞代码:

// MARK: Sections
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return arrayDomande.count
    }

// MARK: Cells
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 1 question & 5 answers
        return 1 + 5
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCellWithIdentifier(QuestionCellIdentifier) as QuestionCellTableViewCell
            configureCell(cell, forTableView: tableView, atIndexPath: indexPath)
            cell.userInteractionEnabled = false

            return cell
        default:
            let cell = tableView.dequeueReusableCellWithIdentifier(AnswerCellIdentifier) as AnswerCellTableViewCell
            configureCell(cell, forTableView: tableView, atIndexPath: indexPath)

            return cell
        }
    }

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCellWithIdentifier(QuestionCellIdentifier) as QuestionCellTableViewCell
            configureCell(cell, forTableView: tableView, atIndexPath: indexPath)
            cell.layoutSubviews()

            return (cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 10.0)
        default:
            let cell = tableView.dequeueReusableCellWithIdentifier(AnswerCellIdentifier) as AnswerCellTableViewCell
            configureCell(cell, forTableView: tableView, atIndexPath: indexPath)
            cell.layoutSubviews()

            return (cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 10.0)
        }
    }

    func configureCell(cell: UITableViewCell, forTableView: UITableView, atIndexPath: NSIndexPath) {
        if (cell.isKindOfClass(QuestionCellTableViewCell)){
            let domanda = arrayDomande[atIndexPath.section]

            var attrs = [NSFontAttributeName: UIFont.boldSystemFontOfSize(19.0)]
            var qString = NSMutableAttributedString(string: domanda.numero.stringValue + ". ", attributes:attrs)
            var dString = NSMutableAttributedString(string: domanda.domanda)
            qString.appendAttributedString(dString)

            (cell as QuestionCellTableViewCell).testoLabel.attributedText = qString
        } else if (cell.isKindOfClass(AnswerCellTableViewCell)) {

            // Answers cache
            // Add answers to arrayRisposte only if aren't already present
            let domanda = arrayDomande[atIndexPath.section]
            if arrayRisposte.indexForKey(atIndexPath.section) == nil {
                let rispXDomanda: [Risposta] = domanda.risposte.allObjects as [Risposta]
                arrayRisposte[atIndexPath.section] = rispXDomanda.shuffled()
            }

            let answers: [Risposta] = arrayRisposte[atIndexPath.section]!
            (cell as AnswerCellTableViewCell).testoLabel.text = answers[atIndexPath.row - 1].risposta
            (cell as AnswerCellTableViewCell).selectionStyle = UITableViewCellSelectionStyle.None
            if (forTableView.indexPathsForSelectedRows() != nil) && (contains(forTableView.indexPathsForSelectedRows() as [NSIndexPath], atIndexPath)) {
                (cell as AnswerCellTableViewCell).accessoryType = UITableViewCellAccessoryType.Checkmark
            } else {
                (cell as AnswerCellTableViewCell).accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }

    // MARK: Select & Deselect
    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        var inpSelectedRow = tableView.indexPathsForSelectedRows()
        if inpSelectedRow != nil {
            for selectedIndexPath in inpSelectedRow! {
                if (selectedIndexPath.section == indexPath.section){
                    tableView.deselectRowAtIndexPath(selectedIndexPath as NSIndexPath, animated: false)
                    tableView.cellForRowAtIndexPath(selectedIndexPath as NSIndexPath)?.accessoryType = UITableViewCellAccessoryType.None
                }
            }
        }

        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
        tableView.cellForRowAtIndexPath(indexPath)?.layoutSubviews()
        return indexPath
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
    }

添加复选标记时,有一种方法可以根据新的单元格高度或固定文本标签的方式更改单元格的高度? 谢谢你们!

安德烈

1 个答案:

答案 0 :(得分:0)

添加复选标记时。它将添加到附件视图中。附件视图需要一些宽度,这会减小单元格中标签的宽度。因此,请相应调整标签框架。