UITableView滚动时如何保持UITableViewCell状态?

吉米李

我有一个关于扩展 UITableViewCell 的问题。
当我单击标题为“显示更多”的 UIButton 时,它正在成功扩展。
然后当我滚动 UITableView 时,我看到的情况是另一个单元格我没有点击它的 UIButton 也展开。
我有什么好主意可以解决这个问题吗?
谢谢。

class ViewController: UIViewController {

let tableView = UITableView()
let cellWithButton = "cellWithButton"
var isExpand: Bool = false
var expandIndexs: [IndexPath] = []
let text = "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.allowsSelection = false
    tableView.separatorInset = .zero
    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.register(WithButtonTableViewCell.self, forCellReuseIdentifier: cellWithButton)


    self.view.addSubview(tableView)

    tableView.snp.makeConstraints { (make) in
        make.top.left.right.bottom.equalToSuperview()
    }
}

@objc func btnPressed(sender: UIButton) {

    let indexPath = IndexPath(row: sender.tag, section: 0)

    if self.isExpand == false {

        self.isExpand = true

        self.expandIndexs.append(indexPath)

    } else {
        self.isExpand = false

        let index = self.expandIndexs.index(of: indexPath)

        if let index = index {
            self.expandIndexs.remove(at: index)
        }
    }

    tableView.beginUpdates()
    tableView.reloadRows(at: [indexPath], with: .none)
    tableView.endUpdates()

}
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

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

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

    let cell = tableView.dequeueReusableCell(withIdentifier: cellWithButton, for: indexPath) as! WithButtonTableViewCell

    if self.expandIndexs.contains(indexPath) {
        cell.cellIsExpand = self.isExpand
    }
    cell.titleLabel.text = text
    cell.expandButton.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
    cell.expandButton.tag = indexPath.row
    cell.layoutIfNeeded()

    return cell
}
}


class WithButtonTableViewCell: UITableViewCell {

var cellIsExpand: Bool = false

let titleLabel: UILabel = { () -> UILabel in
    let ui = UILabel()
    ui.textColor = UIColor.black
    return ui
}()

let expandButton: UIButton = { () -> UIButton in
    let ui = UIButton()
    ui.setTitleColor(UIColor.blue, for: .normal)
    return ui
}()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    loadUI()
    loadLayout()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")

}

override func layoutIfNeeded() {
    super.layoutIfNeeded()

    if cellIsExpand == true {
        titleLabel.numberOfLines = 0
        expandButton.setTitle("Close.", for: .normal)
    }else{
        titleLabel.numberOfLines = 2
        expandButton.setTitle("Show More.", for: .normal)
    }
}

func loadUI() {

    self.addSubview(titleLabel)
    self.addSubview(expandButton)
}

func loadLayout() {

    titleLabel.snp.makeConstraints { (make) in
        make.top.left.equalTo(15)
        make.right.equalTo(-15)
    }

    expandButton.snp.makeConstraints { (make) in
        make.top.equalTo(titleLabel.snp.bottom).offset(10)
        make.left.equalTo(10)
        make.right.equalTo(-15)
        make.bottom.equalTo(-15)            
    }
}
}

图像在这里

你应该有一个 isExpand 的布尔数组,并在 cellForRow 中检查它是否真的改变 row 的高度。

首先制作数组:

var expandingStateArray = [false,false,false,false,false,false,false,false,false,false]

然后在 cellForRows 检查每个单元格的状态:

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

        let cell = tableView.dequeueReusableCell(withIdentifier: cellWithButton, for: indexPath) as! WithButtonTableViewCell

if expandingStateArray[indexPath.row] {
            titleLabel.numberOfLines = 0
            expandButton.setTitle("Close.", for: .normal)
        }else{
            titleLabel.numberOfLines = 2
            expandButton.setTitle("Show More.", for: .normal)
        }
        cell.titleLabel.text = text
        cell.expandButton.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
        cell.expandButton.tag = indexPath.row
        cell.layoutIfNeeded()


        return cell
    }
    }

在按钮的目标方法中写下这个:

@objc func btnPressed(sender: UIButton) {

let indexPath = IndexPath(row: sender.tag, section: 0)

expandingStateArray[sender.tag] = !expandingStateArray[sender.tag]

tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .none)
tableView.endUpdates()

}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

滚动UITableView时,UITableViewCell的选定按钮变为未选定状态

来自分类Dev

使用RecycleView滚动时如何保持Kivy ToggleButton状态?

来自分类Dev

滚动UITableView时UITableViewCell的位置更改

来自分类Dev

UITableView单元格图像在滚动时保持切换单元格的状态

来自分类Dev

如何在滚动时在 UITableViewCell 中保持 UITextView 可编辑并保持其动态大小?

来自分类Dev

UITableView 如何保持滚动到底部[自动滚动]?

来自分类Dev

如何在iOS设备上滚动时使动画Gif保持动画状态?

来自分类Dev

滚动时保持UITableViewCell可见吗?(快速3)

来自分类Dev

如何隐藏滚动条并使内容保持可滚动状态?

来自分类Dev

在顶部添加单元格时,使UITableView保持滚动位置

来自分类Dev

使用 UITableView 时如何保持对象的引用?

来自分类Dev

滚动时如何使导航栏保持固定

来自分类Dev

如何保持滚动时不会移动的列?

来自分类Dev

滚动时如何保持顶部菜单固定

来自分类Dev

滚动时更新UITableViewCell

来自分类Dev

更改ViewControllers时如何保持UISwitch状态?

来自分类Dev

如何在刷新时保持认证状态?

来自分类Dev

如何在刷新时保持认证状态?

来自分类Dev

滚动时UITableView滞后

来自分类Dev

滚动时的UITableView滞后

来自分类Dev

滚动时uitableview崩溃

来自分类Dev

UITableView滚动时滞后

来自分类Dev

活动类保持激活状态,我需要在滚动时保持该激活类不处于活动状态

来自分类Dev

滚动时如何使UITableView从下至上显示?

来自分类Dev

如何使uitableview不能在顶部滚动时被拉

来自分类Dev

UITableView向下滚动时,如何使导航栏高度变小?

来自分类Dev

iOS-滚动UITableView时如何减少UINavigationBar titleView的alpha?

来自分类Dev

滚动UITableView时如何阻止Cell突出显示

来自分类Dev

如何在 UITableView 滚动时缩小 UIImageView 内容(图像)?

Related 相关文章

  1. 1

    滚动UITableView时,UITableViewCell的选定按钮变为未选定状态

  2. 2

    使用RecycleView滚动时如何保持Kivy ToggleButton状态?

  3. 3

    滚动UITableView时UITableViewCell的位置更改

  4. 4

    UITableView单元格图像在滚动时保持切换单元格的状态

  5. 5

    如何在滚动时在 UITableViewCell 中保持 UITextView 可编辑并保持其动态大小?

  6. 6

    UITableView 如何保持滚动到底部[自动滚动]?

  7. 7

    如何在iOS设备上滚动时使动画Gif保持动画状态?

  8. 8

    滚动时保持UITableViewCell可见吗?(快速3)

  9. 9

    如何隐藏滚动条并使内容保持可滚动状态?

  10. 10

    在顶部添加单元格时,使UITableView保持滚动位置

  11. 11

    使用 UITableView 时如何保持对象的引用?

  12. 12

    滚动时如何使导航栏保持固定

  13. 13

    如何保持滚动时不会移动的列?

  14. 14

    滚动时如何保持顶部菜单固定

  15. 15

    滚动时更新UITableViewCell

  16. 16

    更改ViewControllers时如何保持UISwitch状态?

  17. 17

    如何在刷新时保持认证状态?

  18. 18

    如何在刷新时保持认证状态?

  19. 19

    滚动时UITableView滞后

  20. 20

    滚动时的UITableView滞后

  21. 21

    滚动时uitableview崩溃

  22. 22

    UITableView滚动时滞后

  23. 23

    活动类保持激活状态,我需要在滚动时保持该激活类不处于活动状态

  24. 24

    滚动时如何使UITableView从下至上显示?

  25. 25

    如何使uitableview不能在顶部滚动时被拉

  26. 26

    UITableView向下滚动时,如何使导航栏高度变小?

  27. 27

    iOS-滚动UITableView时如何减少UINavigationBar titleView的alpha?

  28. 28

    滚动UITableView时如何阻止Cell突出显示

  29. 29

    如何在 UITableView 滚动时缩小 UIImageView 内容(图像)?

热门标签

归档