Wednesday, February 8, 2023
HomeiOS Developmentios - Caught at reassigning TableView Cell property

ios – Caught at reassigning TableView Cell property


I actually hope somebody may help me, I am caught at this for two days now. Mainly I am engaged on an App the place you may monitor your studying progress for Books.

The HomeViewController incorporates a TableView that lists the books you could have added. It has a progress Bar and exhibits what web page you are on. The AddBookController is for including Information a couple of guide that will get delegated to the HomeViewController and is then listed as a TableView Row. The BookDetailController is proven when you choose a Row and is for updating the Web page you are on. I will present some Screenshots so that you perceive higher.

I am caught at altering the “currentPage” property of the TableView Cell when you replace it within the BookDetailViewController. I will ship the updatedPage to the HomeViewController (the place the TableView is) however I do not know the way to align the values of the Cells with the brand new worth. My concept was to make use of an didSet-observer however I can not work out the way to set it up the fitting means.

Right here is my Code

HomeViewController

    class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SendingBookDataProtocol {
    
        var updatedPage = String() {
            didSet {    // in all probability on the improper place however every little thing I've tried did not work
                print(updatedPage)
                tableView?.reloadData()
            }
        }
    
        
        var objects = [BookItem]()
        
        var merchandise: 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)!
              let consequence = Int(totalPagesInt - currentPageInt)
              let percentageRead = Int((currentPageInt / totalPagesInt) * 100)
              
            bookDetailVc?.lblName = merchandise.title
            bookDetailVc?.lblCurrentPage = Int(Float(merchandise.currentPage)!)
     // ...some extra Code. Mainly simply sending the Information to BookDetailViewController
       
            self.navigationController!.pushViewController(bookDetailVc!, animated: true)
                                         
      }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BookCell", for: indexPath) as! BookCell
            merchandise = objects[indexPath.row]
        
            cell.title.textual content = merchandise?.title
    //... and so forth
                cell.pageNumbers.textual content = "S. " + merchandise!.currentPage + " / " + merchandise!.totalPages //right here currentPage must be up to date
                return cell
            }
        }

BookDetailViewController

class BookDetailViewController: HomeViewController, UIPickerViewDelegate, UIPickerViewDataSource{

    @IBOutlet weak var bookTitle: UILabel!
//...
    @IBOutlet weak var numberPicker: UIPickerView!
    
    var lblName = String()
//...
    var lblCurrentPage = Int()

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        self.numberPicker.delegate = self
        self.numberPicker.dataSource = self
//...
}

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent part: Int) {

            let valueSelected = pickerData[row] as String
        
        if let homeVc = storyboard?.instantiateViewController(withIdentifier: "getBookData") as? HomeViewController{
            homeVc.updatedPage = valueSelected
            homeVc.tableView?.reloadData()
        }
     }
    }

AddBookController

protocol SendingBookDataProtocol {
    func sendDataToHomeController(bookEntry: BookItem)
}

struct BookItem {
    let title,creator,currentPage,totalPages:String
    let picture: UIImage?
}

class AddBookController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var delegate: SendingBookDataProtocol? = nil

 @IBAction func buttonSave(_ sender: Any) {

        let bookEntry = BookItem(title: textfieldTitle.textual content!, creator: textfieldAuthor.textual content!, currentPage: fieldCurrentPage.textual content!, totalPages: fieldTotalPages.textual content!, picture: bookImage)
        
        self.delegate?.sendDataToHomeController(bookEntry: bookEntry)
                  dismiss(animated: true, completion: nil)
     }
}

// relaxation needs to be irrelevant

If anybody may assist me on this, I might be rattling grateful. Listed below are the screenshots

HomeViewController

AddBookController

BookDetailViewController



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments