Выбор отдельной ячейки в табличном представлении в Swift - PullRequest
0 голосов
/ 21 мая 2018

У меня есть табличное представление, в котором у меня есть два раздела.Оба раздела содержат 3 ячейки под ним.Теперь, когда я выбираю любую ячейку, она показывает знак галочки, но выбирает одну ячейку из обоих разделов.Я попробовал некоторый код, но он не работает.Я хочу, чтобы пользователь мог выбрать одну ячейку из обоих разделов, и при загрузке табличного представления его первая ячейка должна быть предварительно выбрана.Как я могу это сделать?Я немного запутался по поводу предварительного выбора ячейки. Мой код для выбора ячейки это,

 self.filterTableView.allowsSelection = true
extension FiltersVC: UITableViewDelegate,UITableViewDataSource{

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

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionTitles[section]
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuItems[section].count
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = filterTableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FiltersTableViewCell

    cell.tilteLbl.text = menuItems[indexPath.section][indexPath.row]
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.accessoryType = cell.isSelected ? .checkmark : .none
    cell.selectionStyle = .none
    cell.backgroundColor = UIColor.clear
    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    headerView.backgroundColor = #colorLiteral(red: 0.9130856497, green: 0.9221261017, blue: 0.9221261017, alpha: 1)

    let headerText = UILabel()
    headerText.textColor = UIColor.black
    headerText.adjustsFontSizeToFitWidth = true
    switch section{
    case 0:
        headerText.textAlignment = .center
        headerText.text = "LIST BY"
        headerText.backgroundColor = #colorLiteral(red: 0.9190355449, green: 0.9281349067, blue: 0.9281349067, alpha: 1)
        headerText.font = UIFont.boldSystemFont(ofSize: 20)
    case 1:
        headerText.textAlignment = .center
        headerText.text = "COUSINE"
        headerText.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
        headerText.font = UIFont.boldSystemFont(ofSize: 20)
    default: break

    }

    return headerText

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = filterTableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark

            let item = menuItems[indexPath.section][indexPath.row]
            UserDefaults.standard.set(item, forKey: "listBy")
            UserDefaults.standard.synchronize()
            filterBtn.isUserInteractionEnabled = true
            filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
        }
    }
    else if indexPath.section == 1{
        if let cell = filterTableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark

            let item = menuItems[indexPath.section][indexPath.row]
            UserDefaults.standard.set(item, forKey: "Cuisine")
            UserDefaults.standard.synchronize()
            filterBtn.isUserInteractionEnabled = true
            filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
        }
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }
    else if indexPath.section == 1{
        if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }

}

Ответы [ 4 ]

0 голосов
/ 01 апреля 2019

indexPaths - это переменная локального массива типа IndexPath, если вы не уверены, что это такое.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.deselectRow(at: indexPath, animated: true)

    if indexPaths.count > 0 {
        tableView.cellForRow(at: indexPaths[0])?.accessoryType = .none

        if indexPaths[0] == indexPath {
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
            self.indexPaths.removeAll()
            return
        }

        self.indexPaths.removeAll()
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        self.indexPaths.append(indexPath)
    } else {
        self.indexPaths.append(indexPath)
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
}
0 голосов
/ 21 мая 2018
  • Включите allowsMultipleSelection для выбора нескольких ячеек одновременно.
  • Используйте tableView (_: willSelectRowAt:) для обработки выбора.Если в | indexPath.section | есть выделенная ячейка, отмените выбор этой строки

    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
      let indexPathsForSelectedRows = tableView.indexPathsForSelectedRows ?? [IndexPath]()
    
      // If there is a selected cell in |indexPath.section|, do nothing
      for selectedIndexPath in indexPathsForSelectedRows {
        if selectedIndexPath.section == indexPath.section {
          tableView.deselectRow(at: selectedIndexPath, animated: true)
        }
      }
    
      return indexPath;
    }
    
0 голосов
/ 21 мая 2018

Вы должны сохранить выбранные индексы где-нибудь, может быть в каком-то массиве с разными разделами.Поскольку вы хотите, чтобы первые ячейки каждой секции были предварительно выбраны, давайте начнем с чего-то вроде этого:

   var selectedIndexes = [[IndexPath.init(row: 0, section: 0)], [IndexPath.init(row: 0, section: 1)]]

В приведенном выше массиве мы сохраняем два пути индекса.Один для первой ячейки первого раздела, а второй для первой ячейки второго раздела.

Теперь ваш cellForRow может проверять существующие пути индекса в массиве следующим образом:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! UITableViewCell
    cell.selectionStyle = .none
    cell.textLabel?.text = tableArray[indexPath.section][indexPath.row]

    let selectedSectionIndexes = self.selectedIndexes[indexPath.section]
    if selectedSectionIndexes.contains(indexPath) {
        cell.accessoryType = .checkmark
    }
    else {
        cell.accessoryType = .none
    }


    return cell
}

Для одиночного выбора:

// For single selection
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    // If current cell is not present in selectedIndexes
    if !self.selectedIndexes[indexPath.section].contains(indexPath) {
        // mark it checked
        cell?.accessoryType = .checkmark

        // Remove any previous selected indexpath
        self.selectedIndexes[indexPath.section].removeAll()

        // add currently selected indexpath
        self.selectedIndexes[indexPath.section].append(indexPath)

        tableView.reloadData()
    }
}

Приведенный выше код удаляет любую ранее выбранную ячейку исохраняет новый.Если одна и та же ячейка выбрана снова и снова, она остается отмеченной.

0 голосов
/ 21 мая 2018

Вы можете попробовать следующий метод, чтобы сохранить одну ячейку выбранной одновременно

**My arrays**

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Peproni", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]

/// Lets keep index Reference for which cell is 
/// Getting selected
var selectedIndex : [Int:Int]?

/// Now in didLoad
override func viewDidLoad() {
   super.viewDidLoad()
   /// Initialise the Dictionary 
   selectedIndex = [Int:Int]()

   /// Set the Default value as in your case
   /// Section - 0 and IndexPath - 0
   /// i.e First cell
   selectedIndex?.updateValue(0, forKey: 0)
   mainTableView.delegate = self
   mainTableView.dataSource = self
   mainTableView.reloadData()
} 

func numberOfSections(in tableView: UITableView) -> Int {
   return section.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    /// my Header cell
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! TableViewCell
    headerCell.titleLabel.text = self.section[section]
    headerCell.ButtonToShowHide.tag = section
    return headerCell.contentView

}

/// Now in CellForRowMethod

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

        cell.textLabel?.text = items[indexPath.section][indexPath.row]

        /// Compare if current section is Available in reference Dict
        if let val = selectedIndex![indexPath.section]{
            /// If Yes
            /// Check IndexPath Row Value
            if indexPath.row == val{
                /// If row is found that is selected
                /// Make it highlight
                /// You can set A radio button
                cell.textLabel?.textColor = UIColor.red
            }
            else{
                /// Set default value for that section
                cell.textLabel?.textColor = UIColor.black
            }
        }
        /// If no
        else{
            /// Required to set Default value for all other section
            /// And
            /// Required to Update previous value if indexPath was selected
            /// In previouus index Section
            cell.textLabel?.textColor = UIColor.black
        }

        return cell
    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        /// Remove All values
        selectedIndex?.removeAll()
        /// Insert current Value
        selectedIndex?.updateValue(indexPath.row, forKey: indexPath.section)
        /// Reload TableView Fully
        /// Or you can keep Reference of Previous and New Value
        /// For not reloading All cells
        self.mainTableView.reloadData()
    }

Выход

, когда TableView загружен

enter image description here

При выборе строки в том же разделе

enter image description here

при выборе строки в другом разделе

enter image description here

GIF Работа - Обновление цвета метки ячейки

enter image description here

Повторное обновление

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

        cell.textLabel?.text = items[indexPath.section][indexPath.row]

        /// Compare if current section is Available in reference Dict
        if let val = selectedIndex![indexPath.section]{
            /// If Yes
            /// Check IndexPath Row Value
            if indexPath.row == val{
                /// If row is found that is selected
                /// Make it highlight
                /// You can set A radio button
                cell.accessoryType = .checkmark
            }
            else{
                /// Set default value for that section
                cell.accessoryType = .none
            }
        }
        /// If no
        else{
            /// Required to set Default value for all other section
            /// And
            /// Required to Update previous value if indexPath was selected
            /// In previouus index Section
            cell.accessoryType = .none
        }

        return cell
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...