简体   繁体   中英

Reassign TableView Cell Variable from another ViewController

I'm working on an App where you can track your reading Progress for Books. I have 3 ViewControllers. One is the HomeViewController, where I have a TableView which displays the book. Second is the AddBookController, where you can enter some data, press a Button and create a new row in the TableView. Third is the BookDetailViewController, which is showing when you click on the selected row. Here I am stuck. There is a button you press and the corresponding TableView Cell should update its page number.

Can you use Notification Center for this? There is no Segue from HomeViewController to BookDetailViewController

I've been trying for hours but can't figure out how to do it. I'd appreciate any help.

HomeViewController

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SendingBookDataProtocol {

    @IBOutlet weak var addBookButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var items = [BookItem]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView?.delegate = self
        tableView?.dataSource = self

        let nib = UINib(nibName: "BookCell", bundle: nil)
        tableView?.register(nib, forCellReuseIdentifier: "BookCell")
    }

    func sendDataToHomeController(bookEntry item:BookItem) {
        items.append(item)
        tableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        items.count
    }
    
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      let bookDetailVc = self.storyboard?.instantiateViewController(withIdentifier: "BookDetailView") as? BookDetailViewController

        let item = items[indexPath.row]
          
        let currentPageInt = Float(item.currentPage)!
        let totalPagesInt = Float(item.totalPages)!
          
          
        bookDetailVc?.lblName = item.title
        bookDetailVc?.lblCurrentPage = item.currentPage
        bookDetailVc?.lblTotalPages = item.totalPages
    
          self.navigationController?.pushViewController(bookDetailVc!, animated: true)
                                     
  }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BookCell", for: indexPath) as! BookCell
        let item = items[indexPath.row]
        
        cell.bookImage.image = item.image
        cell.title.text = item.title
        cell.author.text = item.author
        cell.pageNumbers.text = "P. " + item.currentPage + " / " + item.totalPages
        
        cell.title.text = item.title

        return cell
    }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "getBookData" {
                let addBookVC: AddBookController = segue.destination as! AddBookController
                addBookVC.delegate = self
            }
        }
}

BookDetailView

class BookDetailViewController: HomeViewController{

    @IBOutlet weak var bookTitle: UILabel!
    @IBOutlet weak var currentPageDetail: UILabel!
    @IBOutlet weak var totalPagesDetail: UILabel!

    var lblName = String()
    var lblCurrentPage = String()
    var lblTotalPages = String()


 override func viewDidLoad() {
        super.viewDidLoad()

        bookTitle.text = lblName
        currentPageDetail.text = lblCurrentPage
        totalPagesDetail.text = lblTotalPages

    }

}

your self.navigationController? is null?

try it self.present(bookDetailVc,: animated: true)

I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM