如何在Swift中的表视图控制器中将不同的图像放在文本旁边?

时间:2018-01-17 02:29:38

标签: ios swift uitableview

我遇到了一个问题,我为“快餐”放了一个图像,它显示在我的桌面视图控制器上的所有文本上。我要展示的只是“快餐”,“餐饮”,“服务站”等,它们在桌面视图控制器上有自己的图像。我尝试实现它,但我需要一些帮助。谁能指导我?这是我的代码:

class NearMeIndexTitle {
    var indexTitle = String()
    var imageName = String ()
}

class NearMeIndexTableViewController: UITableViewController  {
    var nearMeIndexes = [NearMeIndexTitle]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Table view list  ..
        let nearmeindex1 = NearMeIndexTitle()
        let nearmeindex1Img = NearMeIndexTitle()

        nearmeindex1.indexTitle = "Fast Food"
        nearmeindex1.imageName = "fastfood.png"

        let nearmeindex2 = NearMeIndexTitle()
        nearmeindex2.indexTitle = "Dining"

        let nearmeIndex3 = NearMeIndexTitle()
        nearmeIndex3.indexTitle = "Service Stations"

        let nearmeIndex4 = NearMeIndexTitle()
        nearmeIndex4.indexTitle = "Hospitals "

        let nearmeIndex5 = NearMeIndexTitle()
        nearmeIndex5.indexTitle = "Grocery"

        nearMeIndexes.append(nearmeindex1)
        nearMeIndexes.append(nearmeindex2)
        nearMeIndexes.append(nearmeIndex3)
        nearMeIndexes.append(nearmeIndex4)
        nearMeIndexes.append(nearmeIndex5)

        self.tableView.reloadData()
   }

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "NearMeIndexCell", for: indexPath) as! NearMeTableViewCell

       let  nearMendexCellValue  = nearMeIndexes[indexPath.row]
       cell.nearMeLabel.text = nearMendexCellValue.indexTitle

       cell.nearMeImageView.image = UIImage(named: nearMendexCellValue.imageName1)

       return cell
    }
}

2 个答案:

答案 0 :(得分:1)

通过对代码进行的更改,您需要为我标记的其他类型设置图像名称。

class NearMeIndexTableViewController: UITableViewController  {
    var nearMeIndexes = [NearMeIndexTitle]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Table view list  ..
        let nearmeindex1 = NearMeIndexTitle()
        let nearmeindex1Img = NearMeIndexTitle()

        nearmeindex1.indexTitle = "Fast Food"
        nearmeindex1.imageName = "fastfood.png"

        let nearmeindex2 = NearMeIndexTitle()
        nearmeindex2.indexTitle = "Dining"
        nearmeindex2.imageName = "dining.png" // here

        let nearmeIndex3 = NearMeIndexTitle()
        nearmeIndex3.indexTitle = "Service Stations"
        nearmeindex3.imageName = "stations.png" // here

        let nearmeIndex4 = NearMeIndexTitle()
        nearmeIndex4.indexTitle = "Hospitals "
        nearmeindex4.imageName = "hospitals.png" // here

        let nearmeIndex5 = NearMeIndexTitle()
        nearmeIndex5.indexTitle = "Grocery"
        nearmeindex5.imageName = "grocery.png" // here

        nearMeIndexes.append(nearmeindex1)
        nearMeIndexes.append(nearmeindex2)
        nearMeIndexes.append(nearmeIndex3)
        nearMeIndexes.append(nearmeIndex4)
        nearMeIndexes.append(nearmeIndex5)

        self.tableView.reloadData()
   }

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "NearMeIndexCell", for: indexPath) as! NearMeTableViewCell

       let  nearMendexCellValue  = nearMeIndexes[indexPath.row]
       cell.nearMeLabel.text = nearMendexCellValue.indexTitle

       cell.nearMeImageView.image = UIImage(named: nearMendexCellValue.imageName1)

       return cell
    }
}

答案 1 :(得分:-1)

你添加了任何约束吗?我使用了故事板,让我的照片完美地贴在标签旁边。

enter image description here