Strange behavior of UITextView in UITableViewCell

I have a cell that contains several views of the stack, the bottom stack element contains a text element and a custom separator.

my cell

I want to create a parameter, when the user clicks on a cell, it displays the entire text of the text view, so the number of maximum lines in this cell is 0, and in other cells should be 3.

I used this tutorial http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/ and I modified it a bit, my code is:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(iden_tableViewCell4) as! TableViewCell4


    if let selectedCellIP = selectedIndexPath {
        if indexPath == selectedCellIP {
            cell.textTextVIew.textContainer.maximumNumberOfLines = 0
        }
        else {
            cell.textTextVIew.textContainer.maximumNumberOfLines = textVIewMaxNumberOfLines
        }

    }
    else {
        cell.textTextVIew.textContainer.maximumNumberOfLines = textVIewMaxNumberOfLines
    }

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    //The user is selecting the cell which is currently expanded
    //we want to minimize it back
    if selectedIndexPath == indexPath {
        selectedIndexPath = nil
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

        return
    }

    //First we check if a cell is already expanded.
    //If it is we want to minimize make sure it is reloaded to minimize it back
    if let selIndexPath = selectedIndexPath {

        let previousPath = selIndexPath
        selectedIndexPath = indexPath
        tableView.reloadRowsAtIndexPaths([previousPath], withRowAnimation: .Fade)

    }


    //Finally set the selected index to the new selection and reload it to expand
    selectedIndexPath = indexPath
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

}

in my viewDidLoadi installed tableView.estimatedRowHeight = 160 tableView.rowHeight = UITableViewAutomaticDimension

, textView . , , . enter image description here

+2
2

UITableViewCell

- . , , , didSelectRowAtIndexPath, .
/ ( selectedIndexPath, ):

Expand

override func tableView(_ tableView: UITableView,
                        didSelectRowAt indexPath: IndexPath) {
    self.reloadRowsAtIndexPaths(tableView,
                                indexPath:indexPath)
}

Contract

override func tableView(_ tableView: UITableView,
                        didDeselectRowAt indexPath: IndexPath) {
    self.reloadRowsAtIndexPaths(tableView,
                                indexPath:indexPath)
}

selectRowAtIndexPath

didDeselectRowAtIndexPath. , , , .

:

, cacheSelectedRows. UITableViewCell. , , reuseIdentifier.

class TableViewController: UITableViewController {
    var cacheSelectedRows:[IndexPath]? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 160
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        if let textLabel = cell.textLabel {
            textLabel.backgroundColor = UIColor.clear

            textLabel.numberOfLines = 1
            if let cacheSelectedRows = cacheSelectedRows {
                textLabel.numberOfLines = (cacheSelectedRows.contains(indexPath)) ? 0 : 1
            }
            textLabel.text = "\(1 + indexPath.row), Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.reloadRowsAtIndexPaths(tableView, indexPath:indexPath)
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
         self.reloadRowsAtIndexPaths(tableView, indexPath:indexPath)
    }

    func reloadRowsAtIndexPaths(_ tableView: UITableView, indexPath: IndexPath) {
        cacheSelectedRows = tableView.indexPathsForSelectedRows

        tableView.reloadRows(at: [indexPath], with: .fade)

        // Restore selection
        if let cacheSelectedRows = cacheSelectedRows {
            for path in cacheSelectedRows {
                self.tableView.selectRow(at: path, animated: false, scrollPosition: .none)
            }
        }
    }
}

Demo

Animated demo


GitHub .

0

, textView , .

, : tableView.estimatedRowHeight = 160 tableView.rowHeight = UITableViewAutomaticDimension textView . cellForRow , , .

, textView StackView, TextView Label . , .

0

Source: https://habr.com/ru/post/1627695/


All Articles