代码之家  ›  专栏  ›  技术社区  ›  dmh

ViewCell height无效Swift 4

  •  -1
  • dmh  · 技术社区  · 7 年前

    我是Xcode的初学者,我需要一些关于表视图中viewcell的帮助。

    这是我的viewcell的属性:

    enter image description here

    当我试着运行模拟器时,它变成了这样:

    enter image description here

    我的viewcontroller

    import UIKit
    import SwiftyJSON
    import Kingfisher
    
    class CakeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
        //MARK : Properties
    
    
        var cakeArray = [Cake]()
    
        @IBOutlet weak var testImage: UIImageView!
        @IBOutlet weak var tableView: UITableView!
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return cakeArray.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cellIdentifier = "CakeTableViewCell"
            //        let cell = tableView.dequeueReusableCell(withIdentifier: "CakeCategoryTableViewCell"/*Identifier*/, for: indexPath)
            guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CakeTableViewCell  else {
                fatalError("The dequeued cell is not an instance of cakeViewCell.")
            }
            //cell.textLabel?.text = cake_category[indexPath.row]
            let cakeObj = cakeArray[indexPath.row]
            cell.cakeLabel.text = cakeObj.product
            return cell
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            print("Load Initial Data")
    
            loadInitialDataFromJson(category : "Reguler Cake")
    
    
            // Do any additional setup after loading the view.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func loadInitialDataFromJson(category:String)
        {
            APIManager.sharedInstance.getCakeByCategory(category: category, onSuccess: {json in DispatchQueue.main.async {
                let status = json["status"].stringValue
                //let message = json["message"].stringValue
    
    
                if status == "OK"
                {
    
                    for (key, subJson) in json["list_produk"] {
                        //print(subJson["id_product"])
                        var arrayVariant = [Variant]()
                        for(key,subsJson) in subJson{
                            arrayVariant.append(Variant(size:subsJson["size"].stringValue,price:subsJson["price"].intValue)!)
                        }
                        self.cakeArray.append(Cake(id_product:subJson["id_product"].stringValue,product:subJson["product"].stringValue,description:subJson["description"].stringValue,imageURL:subJson["images"].stringValue,variant:arrayVariant)!)
                    }
                    self.tableView.rowHeight = UITableViewAutomaticDimension
                    self.tableView.estimatedRowHeight = 150
                    self.tableView.reloadData()
                }else{
                    print("Not Ok")
                }
    
                }}, onFailure: {error in let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
                    self.show(alert, sender: nil)
            })
    
        }
    
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    

    和我的tableviewcell

    import UIKit
    
    
    class CakeTableViewCell: UITableViewCell {
    
        // MARK : Properties
        @IBOutlet weak var cakeImage: UIImageView!
        @IBOutlet weak var cakeLabel: UILabel!
    
    
        @IBAction func addToCart(_ sender: UIButton) {
    
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
    }
    

    知道为什么会这样吗??

    我已经用tableview创建了一些视图,它工作正常。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tarek hemdan    7 年前

    在cakeViewController中调整手机音量

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
        {
            return 100.0;//Choose your custom row height
        }