Home > OS >  Assign tuple array from controller to tuple in tableviewCell
Assign tuple array from controller to tuple in tableviewCell

Time:12-07

var menuListItem: [(title: String, imageName: String, indexValue: Int)] = [
        ("Title Mode", "ModeImage" , 0), ("Contact Us", "ContactUsImage" , 1), ("Logout", "LogoutImage" , 2)]

//Assign Value to SideMenuCell

var sideMenuItem: (title: String, imageName: String, index: Int)?{
    didSet{
        self.setUpData()
    }
}

    func setUpData(){
        lblTitle.text = sideMenuItem?.0
        imageType.image = UIImage.init(named: sideMenuItem?.1 ?? "")
}

//Mark: - TableViewCell

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      if let sideMenuCell = tableView.dequeueReusableCell(withIdentifier: sideMenuCell) as? SideMenuCell{
            sideMenuCell.sideMenuItem = menuListItem[indexPath.row]
            return sideMenuCell
        }
}

I am getting error on this line. sideMenuCell.sideMenuItem = menuListItem[indexPath.row] Cannot assign value of type '(title: String, imageName: String, indexValue: Int)' to type '(title: String, imageName: String, index: Int)'

How to assign tuple [] into UITableViewCell tuple variable.

CodePudding user response:

index is not the same as indexValue. Update your code as follows:

var menuListItems // this is plural. Add an "s".
struct SideMenuItem {
  let title: String
  let imageName: String
  let index: Int
}
var sideMenuItem: SideMenuItem? {
sideMenuCell.sideMenuItem = menuListItems.map(SideMenuItem.init)[indexPath.row]
  • Related