UITableViewのスクロール時にUITableViewCellの状態を維持するにはどうすればよいですか?

ジミーリー

UITableViewCellの拡張について質問があります。
「もっと見る」というタイトルのUIButtonをクリックすると、正常に展開されます。
次に、UITableViewをスクロールすると、クリックしていないもう1つのセルである状況が表示されます。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で、それが実際に行の高さを変更しているかどうかを確認してください。

まず最初に配列を作成します:

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

RecycleViewを使用してスクロールするときにKivyToggleButtonの状態を維持するにはどうすればよいですか?

分類Dev

ループ中にスキャナーがクリアされました:特定の状態を維持するにはどうすればよいですか?

分類Dev

ブロードリーフコマースを開始するたびに、DBの状態を維持するにはどうすればよいですか?

分類Dev

スクロール時にUITableViewCellでUITextViewを編集可能に保ち、動的サイズを維持するにはどうすればよいですか?

分類Dev

ルート変更後もsvelteストアの状態を維持するにはどうすればよいですか?

分類Dev

VIMのNERDTreeに以前の状態を維持させるにはどうすればよいですか?

分類Dev

ホットリロード時にプロバイダーを使用してFlutterGlobal BloC状態を維持するにはどうすればよいですか?

分類Dev

最新のメインラインカーネルで自動的に最新の状態を維持するにはどうすればよいですか?

分類Dev

最新のメインラインカーネルで自動的に最新の状態を維持するにはどうすればよいですか?

分類Dev

async-selectにフォーカスを失った後、オプションの状態を維持するにはどうすればよいですか?

分類Dev

GridViewに動的行を追加するときにページのスクロール位置を維持するにはどうすればよいですか?

分類Dev

Android StudioでUSBを抜く前に、ホットデプロイの状態を維持するにはどうすればよいですか?

分類Dev

react-routerとreduxを使用しているときにストアの状態を維持するにはどうすればよいですか?

分類Dev

ファイルの追跡を停止し、Gitで現在の状態を維持するにはどうすればよいですか?

分類Dev

異なるページ間でボタンの状態を維持するにはどうすればよいですか?

分類Dev

スクロール時に移動しない列を維持するにはどうすればよいですか?

分類Dev

デバイス間でアプリ内購入の状態を維持し、復元するにはどうすればよいですか

分類Dev

ReactJS-ルート変更時に状態を維持して反応するためのページネーションを実装するにはどうすればよいですか?

分類Dev

検証が必要なwinformsDBアプリケーションで揮発性の状態を維持するにはどうすればよいですか?

分類Dev

UITableviewのスクロールの違いを見つけるにはどうすればよいですか-迅速

分類Dev

状態を維持しながらコンポーネントの順序を変更するにはどうすればよいですか?

分類Dev

状態を維持しながらコンポーネントの順序を変更するにはどうすればよいですか?

分類Dev

再帰的に機能するプール内のグローバルプロセスを維持するにはどうすればよいですか?

分類Dev

StackLayoutでXamarinFormsコントロールのアスペクト比を維持するにはどうすればよいですか?

分類Dev

Angularjs UIルーターの状態リストを取得するにはどうすればよいですか?

分類Dev

親を変更するときに「GlobalKey」を使用してウィジェットの状態を維持するにはどうすればよいですか?

分類Dev

クリック時に入力グループ内の変数の状態を設定するにはどうすればよいですか?

分類Dev

React Routerで、ページを更新してもログイン状態を維持するにはどうすればよいですか?

分類Dev

UITableViewのスクロールを停止させるにはどうすればよいですか

Related 関連記事

  1. 1

    RecycleViewを使用してスクロールするときにKivyToggleButtonの状態を維持するにはどうすればよいですか?

  2. 2

    ループ中にスキャナーがクリアされました:特定の状態を維持するにはどうすればよいですか?

  3. 3

    ブロードリーフコマースを開始するたびに、DBの状態を維持するにはどうすればよいですか?

  4. 4

    スクロール時にUITableViewCellでUITextViewを編集可能に保ち、動的サイズを維持するにはどうすればよいですか?

  5. 5

    ルート変更後もsvelteストアの状態を維持するにはどうすればよいですか?

  6. 6

    VIMのNERDTreeに以前の状態を維持させるにはどうすればよいですか?

  7. 7

    ホットリロード時にプロバイダーを使用してFlutterGlobal BloC状態を維持するにはどうすればよいですか?

  8. 8

    最新のメインラインカーネルで自動的に最新の状態を維持するにはどうすればよいですか?

  9. 9

    最新のメインラインカーネルで自動的に最新の状態を維持するにはどうすればよいですか?

  10. 10

    async-selectにフォーカスを失った後、オプションの状態を維持するにはどうすればよいですか?

  11. 11

    GridViewに動的行を追加するときにページのスクロール位置を維持するにはどうすればよいですか?

  12. 12

    Android StudioでUSBを抜く前に、ホットデプロイの状態を維持するにはどうすればよいですか?

  13. 13

    react-routerとreduxを使用しているときにストアの状態を維持するにはどうすればよいですか?

  14. 14

    ファイルの追跡を停止し、Gitで現在の状態を維持するにはどうすればよいですか?

  15. 15

    異なるページ間でボタンの状態を維持するにはどうすればよいですか?

  16. 16

    スクロール時に移動しない列を維持するにはどうすればよいですか?

  17. 17

    デバイス間でアプリ内購入の状態を維持し、復元するにはどうすればよいですか

  18. 18

    ReactJS-ルート変更時に状態を維持して反応するためのページネーションを実装するにはどうすればよいですか?

  19. 19

    検証が必要なwinformsDBアプリケーションで揮発性の状態を維持するにはどうすればよいですか?

  20. 20

    UITableviewのスクロールの違いを見つけるにはどうすればよいですか-迅速

  21. 21

    状態を維持しながらコンポーネントの順序を変更するにはどうすればよいですか?

  22. 22

    状態を維持しながらコンポーネントの順序を変更するにはどうすればよいですか?

  23. 23

    再帰的に機能するプール内のグローバルプロセスを維持するにはどうすればよいですか?

  24. 24

    StackLayoutでXamarinFormsコントロールのアスペクト比を維持するにはどうすればよいですか?

  25. 25

    Angularjs UIルーターの状態リストを取得するにはどうすればよいですか?

  26. 26

    親を変更するときに「GlobalKey」を使用してウィジェットの状態を維持するにはどうすればよいですか?

  27. 27

    クリック時に入力グループ内の変数の状態を設定するにはどうすればよいですか?

  28. 28

    React Routerで、ページを更新してもログイン状態を維持するにはどうすればよいですか?

  29. 29

    UITableViewのスクロールを停止させるにはどうすればよいですか

ホットタグ

アーカイブ