简体   繁体   English

UITableViewCell高度不随定制高度而变化

[英]UITableViewCell height not changing with custom height

when I run my program, the table cell is at the default 44, even though I have it set to 68 in my program. 当我运行程序时,表单元格默认为44,即使我在程序中将其设置为68。 I'm using the same TableViewCell for two different TableViewControllers. 我对两个不同的TableViewControllers使用相同的TableViewCell。 At run time, one of them is showing up with the custom height of 68, but the other changes to the default 44. 在运行时,其中一个显示的自定义高度为68,而另一个更改为默认高度44。

在此处输入图片说明

在此处输入图片说明

I've included images of what happens to each table at run time. 我提供了运行时每个表发生什么情况的图像。 I am also getting the following error: 我也收到以下错误:

 Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fixes it. ( "<NSLayoutConstraint:0x600000096ad0 UIButton:0x7f7fead24c10.width == 44 (active)>", "<NSLayoutConstraint:0x608000094be0 'UISV-canvas-connection' FoodTracker.RatingControl:0x7f7fead25800.leading == UIButton:0x7f7fead24c10.leading (active)>", "<NSLayoutConstraint:0x60800008b130 'UISV-canvas-connection' H:[UIButton:0x7f7fead24c10]-(0)-| (active, names: '|':FoodTracker.RatingControl:0x7f7fead25800 )>", "<NSLayoutConstraint:0x60800008c120 'UIView-Encapsulated-Layout-Width' FoodTracker.RatingControl:0x7f7fead25800.width == 56 (active)>" ) class MealTableViewController: UITableViewController { //MARK: Properties @IBOutlet weak var menuButton: UIBarButtonItem! var meals = [Meal]() override func viewDidLoad() { super.viewDidLoad() if self.revealViewController() != nil { menuButton.target = self.revealViewController() menuButton.action = #selector(SWRevealViewController.revealToggle(_:)) self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) } loadSampleMeals() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return meals.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // Table view cells are reused and should be dequeued using a cell identifier. let cellIdentifier = "MealTableViewCell" guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MealTableViewCell else { fatalError("The dequeued cell is not an instance of MealTableViewCell.") } // Fetches the appropriate meal for the data source layout. let meal = meals[indexPath.row] cell.nameLabel.text = meal.name cell.priceLabel.text = meal.price cell.photoImageView.image = meal.photo cell.ratingControl.rating = meal.rating return cell } /* // Override to support conditional editing of the table view. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } */ /* // Override to support editing the table view. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Delete the row from the data source tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the item to be re-orderable. return true } */ //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?) { super.prepare(for: segue, sender: sender) switch(segue.identifier ?? "") { case "ShowDetail": guard let mealDetailViewController = segue.destination as? MealViewController else { fatalError("Unexpected destination: \\(segue.destination)") } guard let selectedMealCell = sender as? MealTableViewCell else { fatalError("Unexpected sender: \\(sender)") } guard let indexPath = tableView.indexPath(for: selectedMealCell) else { fatalError("The selected cell is not being displayed by the table") } let selectedMeal = meals[indexPath.row] mealDetailViewController.meal = selectedMeal default: fatalError("Unexpected Segue Identifier; \\(segue.identifier)") } } //MARK: Private Methods /* // Follow this format guard let meal1 = Meal(name: "", price: "$", photo: , rating: , calories: "Calories: ", description: "Description: ") else { fatalError("Unable to instantiate meal1") } */ private func loadSampleMeals() { let photo1 = UIImage(named: "Sprite") let photo2 = UIImage(named: "HotCheetos") let photo3 = UIImage(named: "Nachos") guard let meal1 = Meal(name: "Sprite", price: "$1.50", photo: photo1, rating: 0, calories: "Calories: 129", description: "Description: A refreshing lemon-lime soda") else { fatalError("Unable to instantiate meal1") } guard let meal2 = Meal(name: "Hot Cheetos", price: "$1.50", photo: photo2, rating: 0, calories: "Calories: 160", description: "Description: A spicy version of original cheetos") else { fatalError("Unable to instantiate meal2") } guard let meal3 = Meal(name: "Nachos", price: "$1.50", photo: photo3, rating: 0, calories: "Calories: 436", description: "Description: Tortilla chips with a side of smooth nacho cheese") else { fatalError("Unable to instantiate meal2") } meals += [meal1, meal2, meal3] } } Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600000096ad0 UIButton:0x7f7fead24c10.width == 44 (active)> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

I am using auto layout and have put no constraints on. 我正在使用自动布局,并且没有任何限制。 I am very confused why one table resets to default sizing even though they are the same class 我很困惑,为什么一个表重置为默认大小,即使它们是同一类

I have added the code for the TableViewController below the error message 我在错误消息下方添加了TableViewController的代码

Click on your UITableViewCell, open the right pane, and in the Size Inspector make sure the Custom checkbox is checked next to Row Height. 单击UITableViewCell,打开右窗格,然后在“大小”检查器中,确保选中“行高”旁边的“自定义”复选框。 The value you specify here is a default cell height, but will only be overridden if the Custom box is checked and you have defined heightForRowAt indexPath. 您在此处指定的值是默认的单元格高度,但是只有在选中“自定义”框并且定义了heightForRowAt indexPath时,该值才会被覆盖。

If you're still getting the same error, there must be something else completely unrelated that is breaking. 如果仍然遇到相同的错误,则必须存在其他完全不相关的东西正在中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM