Monday, September 26, 2022
HomeiOS Developmentios - Reassign TableView Cell Variable from one other ViewController

ios – Reassign TableView Cell Variable from one other ViewController


I am engaged on an App the place you’ll be able to observe your studying Progress for Books. I’ve 3 ViewControllers. One is the HomeViewController, the place I’ve a TableView which shows the guide. Second is the AddBookController, the place you’ll be able to enter some information, press a Button and create a brand new row within the TableView. Third is the BookDetailViewController, which is displaying while you click on on the chosen row. Right here I’m caught. There’s a button you press and the corresponding TableView Cell ought to replace its web page quantity.

Can you employ Notification Middle for this? There isn’t any Segue from HomeViewController to BookDetailViewController

I have been making an attempt for hours however cannot work out learn how to do it. I might recognize any assist.

HomeViewController

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SendingBookDataProtocol {

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

    override func viewDidLoad() {
        tremendous.viewDidLoad()

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

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

    func sendDataToHomeController(bookEntry merchandise:BookItem) {
        objects.append(merchandise)
        tableView.reloadData()
    }

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

        let merchandise = objects[indexPath.row]
          
        let currentPageInt = Float(merchandise.currentPage)!
        let totalPagesInt = Float(merchandise.totalPages)!
          
          
        bookDetailVc?.lblName = merchandise.title
        bookDetailVc?.lblCurrentPage = merchandise.currentPage
        bookDetailVc?.lblTotalPages = merchandise.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 merchandise = objects[indexPath.row]
        
        cell.bookImage.picture = merchandise.picture
        cell.title.textual content = merchandise.title
        cell.creator.textual content = merchandise.creator
        cell.pageNumbers.textual content = "P. " + merchandise.currentPage + " / " + merchandise.totalPages
        
        cell.title.textual content = merchandise.title

        return cell
    }

        override func put together(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "getBookData" {
                let addBookVC: AddBookController = segue.vacation spot 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() {
        tremendous.viewDidLoad()

        bookTitle.textual content = lblName
        currentPageDetail.textual content = lblCurrentPage
        totalPagesDetail.textual content = lblTotalPages

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular