带图像和对勾的iOS自定义TableView

时间:2018-10-31 18:05:51

标签: ios swift uitableview

我正在尝试复制提供的链接中的内容。我正在尝试显示带有可以通过检查IOS应用程序的图像的Tableview。我不太确定自己在做什么而不显示图像。我确保将图像添加到资产文件夹,并在ViewController文件中正确调用了它们。这是我文件的代码:

import UIKit

class CustomCell : UITableViewCell{
    @IBOutlet var myImage: UIImageView!
    @IBOutlet var myText: UILabel!
}

class RmMatchingViewControllor : UIViewController, UITableViewDelegate, UITableViewDataSource{ //
    @IBOutlet var tableView: UITableView!

    var labelData = ["Social Drinking", "Clean Freak", " Do you Cook", "Enjoy Movies", "Listen to Music", "Enjoy Sports","Perefer temperature cold", "Non smoking"]
    var imageData = ["Beer", "Clean", "cooking", "movies", "Music","NFL","roomTemp", "Smoking"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell:CustomCell = self.tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell //check this spot if program doesnt run
        cell.myText?.text = self.labelData[indexPath.row]
        cell.myImage?.image = UIImage(named:self.imageData[indexPath.row])
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return labelData.count
    }

    func numberOfSections(in tableView: UITableView) -> Int{
        return 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        if let cell = tableView.cellForRow(at: indexPath as IndexPath){
            if cell.accessoryType == .checkmark{
                cell.accessoryType = .none
            } else{
                cell.accessoryType = .checkmark
            }
        }
    }
}

Here is the picture of the ViewControllor

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

@Tyrek Clarke尝试在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)处添加一个断点,查看它是否正在被调用或正确分配了图像。您通常可以检查UIImage的输出。我尝试使用相同的代码,并能够产生结果。 (打算错过一些图像)

Result

要注意的事情:

  • 您的表视图委托和数据源已连接
  • 您的单元格重用标识符应与您在代码中编写的标识符相同
  • 确保已连接您的IBOutlets
  • 检查图像名称(确保没有区别)

答案 1 :(得分:0)

enter image description here var slotCellArray:[SlotCell] = SlotCell

func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {         返回SlotObjectArray.count     }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell
     = tableView.dequeueReusableCell(withIdentifier: "SlotCell_id", for: indexPath) as! SlotCell

    cell.lbl_slot_details.text = SlotObjectArray[indexPath.row].Time

    if (SlotObjectArray[indexPath.row].Available_Status as? Bool)! {

       cell.btn_slot_status_out.setTitle(" ", for: .normal)

    } else {

         cell.btn_slot_status_out.setTitle("✔️", for: .normal)
    }
    cell.btn_slot_status_out.tag = indexPath.row


    self.slotCellArray.append(cell)

    return cell




}


@IBAction func btn_slot_status_act(_ sender: UIButton) {

if sender.titleLabel?.text == "✔️"
{
         SlotObjectArray[sender.tag].Available_Status = true

    for i in 0..<selectedSlots.count {
      print(i)
        if let slotSelected = selectedSlots[i] as? AnyObject {

            if let SlotId = slotSelected["slot_id"] as? String {

                if SlotId == SlotObjectArray[sender.tag].id {
                    selectedSlots.remove(at: i)
                    break
                }

            }

        }

    }



    print(selectedSlots)

} else {

      SlotObjectArray[sender.tag].Available_Status = false
    let slot = ["Slot_id":SlotObjectArray[sender.tag].id,"Selected_time":SlotObjectArray[sender.tag].Time,"Booking_date":txt_date.text!]

    selectedSlots.append(slot as AnyObject)


     print(selectedSlots)
    }
   tbl_slots_list.reloadData()

}[![enter image description here][1]][1]