我的代码中的自定义TableView +段控制器的格式和错误

时间:2017-07-30 23:20:41

标签: swift tableview uisegmentedcontrol

所以我创建了一个具有两个TableView和一个Segment Control的View Controller。下面的代码给了我两个错误。

  1. (return serviceImage.count)给出了一个错误,说明返回后的代码永远不会被执行。
  2. 2.(cell.myReviewPic!.image = UIImage(named:reviewPhoto [indexPath.row]))在解包可选值时返回意外发现的错误

    我的代码

    @IBAction func segmentedControlChanged(_ sender: Any) {
        myTableServiceU.reloadData()
        myTableService.reloadData()
    }
    
    
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBOutlet weak var myTableServiceU: UITableView!
    @IBOutlet weak var myTableService: UITableView!
    
    let serviceName: [String] = ["Whataburger", "Red Lobster", "Pizzahut", "McDonalds", "Burger King", "Sushi Garden", "In & Out Burger", "Canes", "Jimmy Johns"]
    
    let serviceImage: [String] = ["place1,", "place2", "plac3", "place4", "place5", "place6", "place7", "place8", "place9"]
    
    let serviceNumber: [String] = ["38", "35", "29", "25", "23", "19", "17", "12", "11"]
    
    let brandPhoto: [String] = ["TK", "MC", "BA"]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        myTableService.dataSource = self
        myTableService.delegate = self
    
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        switch(segmentControl.selectedSegmentIndex)
        {
        case 0:
            return  brandPhoto.count
        case 1:
            return  serviceName.count
            return  serviceImage.count
            return  serviceNumber.count
            break
        default:
            break
        }
    
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ServiceCell
    
    
        switch(segmentControl.selectedSegmentIndex)
        {
        case 0:
            cell.myReviewPic.image = UIImage(named: brandPhoto[indexPath.row])
            break
    
        case 1:
            cell.myServiceName.text = serviceName[indexPath.row]
            cell.myServiceImage.image = UIImage(named: serviceImage[indexPath.row])
            cell.myServiceNumber.text = serviceNumber[indexPath.row]
            break
    
        default:
            break
        }
    
        return cell
    }
    

    }

1 个答案:

答案 0 :(得分:0)

return  serviceName.count
return  serviceImage.count // Line will be ignored
return  serviceNumber.count // Line will be ignored
  返回后的

[...]代码永远不会被执行。

这只是一个警告,因为只有serviceName数组才会返回它的数量。


cell.myReviewPic!.image = UIImage(named: reviewPhoto[indexPath.row]))
  

在展开可选值时意外发现nil

myReviewPic为零,您正尝试使用!强制解包它。可能无法通过IBOutlet连接到ServiceCell nib / xib。