简体   繁体   中英

Assign tuple array from controller to tuple in tableviewCell

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.

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]

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