UITableViewCell高度不随自定义高度而变化

时间:2017-04-11 19:55:52

标签: ios css uitableview

当我运行程序时,表格单元格默认为44,即使我在程序中将其设置为68。我正在为两个不同的TableViewControllers使用相同的TableViewCell。在运行时,其中一个显示自定义高度为68,但另一个更改为默认值44。

enter image description here

enter image description here

我已经包含了运行时每个表会发生什么的图像。我也收到以下错误:

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.

我正在使用自动布局并且没有任何限制。我很困惑为什么一个表重置为默认大小,即使它们是同一个类

我在错误消息

下面添加了TableViewController的代码

1 个答案:

答案 0 :(得分:1)

单击您的UITableViewCell,打开右侧窗格,然后在“大小”检查器中,确保“行高”旁边的“自定义”复选框选中。您在此处指定的值是默认单元格高度,但只有在选中“自定义”框并且您已定义heightForRowAt indexPath时才会覆盖该值。

如果您仍然遇到同样的错误,那么必须有其他完全不相关的错误。