Home > database >  Table View Cell has no Initialiser
Table View Cell has no Initialiser

Time:02-20

I am trying to display table view cell property into different table view cell when button I clicked . I am using story board . I added the Horizontal and vertical stack to contains the image and label properties . I want to display this property into another table view cell when user clicked the show button . In cellFor row function i defined the button action property . I am getting following error .Class 'DetailsViewCell' has no initializers. Cannot use instance member 'mc' within property initializer; property initializers run before 'self' is available

Here is the screenshot of the both view controller . enter image description here

Here is the code .

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        guard let cell = tableView.dequeueReusableCell(withIdentifier: MovieCell.identifier, for: indexPath) as? MovieCell
//        else { return UITableViewCell() }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: MovieViewCell.identifier, for: indexPath) as! MovieViewCell
        
        
        cell.showButton.tag = indexPath.row
        cell.showButton.addTarget(self, action: Selector("movieDetails"), for: .touchUpInside)
        
        let row = indexPath.row
        let title = presenter.getTitle(by: row)
        let overview = presenter.getOverview(by: row)
        let data = presenter.getImageData(by: row)
        cell.configureCell(title: title, overview: overview, data: data)
        return cell
    }
} 

Here is the code in identifier class with table view cell.

class MovieViewCell: UITableViewCell {
    
    static let identifier = "MovieViewCell"
    
    @IBOutlet weak var mainStackView: UIStackView!
    
    @IBOutlet weak var movieImage: UIImageView!
    
    @IBOutlet weak var movieTtile: UILabel!
    
    @IBOutlet weak var movieOverview: UILabel!
    
    @IBOutlet weak var showButton: UIButton!
    
    @IBAction func movieDetails(_ sender: UIButton) {

        
        var  dc : DetailsViewCell
        movieTtile = dc.movieTitle
        movieOverview = dc.movieOverview
        movieImage = dc.movieImage
        
        
    }
    
    
    func configureCell(title: String?, overview: String?, data: Data?) {
        
        movieTtile.text = title
        movieOverview.text = overview
        
        if let imageData = data{
            movieImage.image = UIImage(data: imageData)
        }
    }
}

Here is the code for Details view cell.

class DetailsViewCell: UITableViewCell {
    
    @IBOutlet weak var movieTitle: UILabel!
    
    @IBOutlet weak var movieOverview: UILabel!
    
    @IBOutlet weak var movieImage: UIImageView!
    

         var mc : MovieViewCell
        
         movieTitle = mc.movieTtile
         movieOverview = mc.movieOverview
         movieImage = mc.movieImage
        
    
    
}
   

  

CodePudding user response:

import UIKit

var loginData = ["", "", ""]

class LoginDataCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var txtLoginData: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
    if textField.tag == 0 {
       loginData[0] = textField.text
    } else if textField.tag == 1 {
      loginData[1] = textField.text
    } else if textField.tag == 2 {
      loginData[2] = textField.text
    }
    }

}
  • Related