Home > Blockchain >  Cannot add cell to table view
Cannot add cell to table view

Time:01-17

I'm new to this, I've just made an indexed table view "groups search" with all groups in my app and got a problem: when I'm trying to add some group to the "my groups" view, there are should appear a selected group, but actually I got the first one from all groups array instead. Also I can't add several items started with a similar letter in the "my groups". It might be stupid, but I have no idea how to fix that. Thank you!

import UIKit

final class AllGroupsViewController: UITableViewController {

 var groups = [
     "cats",
     "birds",
     "dogs",
     "books",
     "music",
     "movies",
     "art",
     "science",
     "tech",
     "beauty",
 ]
 var groupSectionTitles = [String]()
 var groupsDictionary = [String: [String]]()

 // MARK: - Lifecycle
 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.register(UINib(
         nibName: "GroupCell",
         bundle: nil),
                        forCellReuseIdentifier: "groupCell")
     
     for group in groups {
         let groupKey = String(group.prefix(1))
         if var groupValues = groupsDictionary[groupKey] {
             groupValues.append(group)
             groupsDictionary[groupKey] = groupValues
         } else {
             groupsDictionary[groupKey] = [group]
         }
     }
     
     
     groupSectionTitles = [String](groupsDictionary.keys)
     groupSectionTitles = groupSectionTitles.sorted(by: { $0 < $1 })
     
 }
 
 // MARK: - Table view data source
 override func numberOfSections(in tableView: UITableView) -> Int {
     return groupSectionTitles.count
 }
 
 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     
     let groupKey = groupSectionTitles[section]
     if let groupValues = groupsDictionary[groupKey] {
         return groupValues.count
     }
     
     return 0
 }
 
 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
     return groupSectionTitles[section]
 }
 
 override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
     return groupSectionTitles
 }
 
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     guard
         let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell", for: indexPath) as? GroupCell
     else { return UITableViewCell() }
     
     var currentGroup = groups[indexPath.row]
     
     let groupKey = groupSectionTitles[indexPath.section]
     if let groupValues = groupsDictionary[groupKey] {
         currentGroup = groupValues[indexPath.row]
     }
     
     cell.configure(
         photo: UIImage(systemName: "person.3.fill") ?? UIImage(),
         name: currentGroup)

     return cell
 }

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     defer { tableView.deselectRow(
         at: indexPath,
         animated: true) }
     performSegue(
         withIdentifier: "addGroup",
         sender: nil)
 }

}


import UIKit

final class MyGroupsViewController: UITableViewController {
    var groups = [String]() {
        didSet {
            //
        }
    }
    
    
@IBAction func addGroup(segue: UIStoryboardSegue) {
    guard
        segue.identifier == "addGroup",
        let allGroupsController = segue.source as? AllGroupsViewController,
        let groupIndexPath = allGroupsController.tableView.indexPathForSelectedRow,
        !self.groups.contains(allGroupsController.groups[groupIndexPath.section])
    else { return }
    self.groups.append(allGroupsController.groups[groupIndexPath.section])
    tableView.reloadData()
}
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(
            nibName: "GroupCell",
            bundle: nil),
                           forCellReuseIdentifier: "groupCell")
    }
    
    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        groups.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard
            let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell", for: indexPath) as? GroupCell
        else { return UITableViewCell() }
        
        let currentGroup = groups[indexPath.row]

        cell.configure(
            photo: UIImage(systemName: "person.3.fill") ?? UIImage(),
            name: currentGroup)

        return cell
    }

    override func tableView(
        _ tableView: UITableView,
        commit editingStyle: UITableViewCell.EditingStyle,
        forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            groups.remove(at: indexPath.row)
            tableView.deleteRows(
                at: [indexPath],
                with: .fade)
        }
    }

}


CodePudding user response:

First here is what I understand the goal is:

  1. There is a UITableViewController called MyGroupsViewController which should show all the groups the user has selected
  2. On tapping on from the MyGroupsViewController, the user is taken to another UITableViewController called AllGroupsViewController which shows all the groups the user can join
  3. On selecting a UITableViewCell from AllGroupsViewController, you will unwind back to MyGroupsViewController showing the groups added for the user

I will say you were quite close and I have just added some minor things which I hope will bring you close to your goal.

One way to pass data between UIViewControllers when using segue transitions is to override a function called Passing data between UITableViewControllers using segues here

You can watch the experience here on Youtube

If I missed something or did not answer something from your question, please add some more info / questions in the comments.

  •  Tags:  
  • Related