Hoşgeldin. Soru sormak veya cevaplamak için hemen üye ol.

Table View'da isEditing modunda checkmark ve çoklu seçim

0 oy
35 kez görüntülendi
22, Nisan, 22 ios development kategorisinde csayin tarafından soruldu
22, Nisan, 22 csayin tarafından düzenlendi

Merhabalar,

iOS mail uygulamasındaki gibi navigation bara bir button koydum ve aşağıdaki gibi tableview'ın isEditing modunu aktif hale getiriyorum, 'allowsMultipleSelectionDuringEditing = true' da true ya çekiyorum ancak ne row'a tıkladığımda unselected olarak gelen checkmarklar selected oluyor nede çoklu seçim ile ilgili bir işlem yapamıyorum. Tableview'da custom bir tableviewcell kullanıyorum sorun bununla mi alakalı çözüm bulamadım. Çoklu seçim ve selected/unselected checkmark olayını nasıl halledebilirim?

   @objc func edit(_ sender: UIBarButtonItem){

        _tblLastTransaction.isEditing = !_tblLastTransaction.isEditing

        if self._tblLastTransaction.isEditing{
            sender.title = "Tamam"

            let leftBarButtonItem = UIBarButtonItem(title: "Tümünü Seç", style: .plain, target: self, action: #selector(LastTransactionVC.selectA))
            self.navigationItem.leftBarButtonItem = leftBarButtonItem

            _tblLastTransaction.allowsMultipleSelectionDuringEditing = true
            _tblLastTransaction.setEditing(true, animated: false)

        }
        else{
            sender.title = "Düzenle"
            self.navigationItem.leftBarButtonItem = .none
        }

    }

enter image description here

1 cevap

0 oy
23, Nisan, 23 yasin tarafından cevaplandı

Karşılaştığın problemin tam olarak neyden kaynaklandığını anlamadım, kodun tamamını incelemek gerekebilir. Ama yapmak istediğine yakın bir örnek hazırladım. Belki karşılaştırarma yaparak problemin ne olduğunu bulabilirsin.

//
//  TableTestViewController.swift
//  splitViewTest
//
//  Created by Yasin TURKOGLU on 23.04.2020.
//  Copyright © 2020 Yasin TURKOGLU. All rights reserved.
//

import UIKit

class TableTestViewController: UIViewController {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.separatorInset = .zero
        tableView.separatorStyle = .singleLine
        tableView.allowsMultipleSelectionDuringEditing = true
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()
    private var isAllRowsSelected: Bool = false
    private var selectedRowsIndexPath = [IndexPath]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Table Test"
        editingStateUpdater()
        tableViewSetup()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if tableView.isEditing {
            tableView.isEditing = true
            editingStateUpdater()
        }
    }

    private func tableViewSetup() {
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        view.layoutIfNeeded()
        tableView.reloadData()
    }

    @objc private func editToggle() {
        tableView.isEditing = !tableView.isEditing
        editingStateUpdater()
    }

    @objc private func sellectAllToggle() {
        isAllRowsSelected = !isAllRowsSelected
        allRowsSelection(selectAll: isAllRowsSelected)
    }

    private func allRowsSelection(selectAll: Bool) {
        for secionIndex in 0..<tableView.numberOfSections {
            let rows = tableView.numberOfRows(inSection: secionIndex)
            for rowIndex in 0..<rows {
                let indexPath = IndexPath(row: rowIndex, section: secionIndex)
                if selectAll {
                    selectedRowsIndexPath.append(indexPath)
                }
                if let rowAtIndexPath = tableView.cellForRow(at: indexPath) {
                    if !selectAll {
                        tableView.deselectRow(at: indexPath, animated: true)
                    } else {
                        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
                    }
                }
            }
        }
        if !selectAll {
            selectedRowsIndexPath.removeAll()
        }
    }

    private func editingStateUpdater() {
        var rightBarButtonTitle: String = "Düzenle"
        isAllRowsSelected = false
        selectedRowsIndexPath.removeAll()
        allRowsSelection(selectAll: isAllRowsSelected)
        if tableView.isEditing {
            rightBarButtonTitle = "Tamam"
            tableView.setEditing(true, animated: true)
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Tümünü Seç", style: .plain, target: self, action: #selector(sellectAllToggle))
        } else {
            tableView.setEditing(false, animated: true)
            navigationItem.leftBarButtonItem = nil
        }
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: rightBarButtonTitle, style: .plain, target: self, action: #selector(editToggle))
    }

}

extension TableTestViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.textColor = .black
        cell.textLabel?.text = "Row \(indexPath.row)"
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if !tableView.isEditing {
            tableView.deselectRow(at: indexPath, animated: true)
        } else {
            if !selectedRowsIndexPath.contains(indexPath) {
                selectedRowsIndexPath.append(indexPath)
            }
        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if tableView.isEditing {
            if isAllRowsSelected {
                isAllRowsSelected = false
            }
            if let index = selectedRowsIndexPath.firstIndex(of: indexPath) {
                selectedRowsIndexPath.remove(at: Int(index.magnitude))
            }
        }
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if tableView.isEditing {
            if selectedRowsIndexPath.contains(indexPath) {
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
            } else {
                tableView.deselectRow(at: indexPath, animated: false)
            }
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50.0
    }

}
...