无法将我的CustomCell.xib连接到情节提要中的tableView

时间:2019-06-19 19:19:28

标签: ios swift

我必须做一个食谱应用程序,它会在tableView中向我显示不同的食谱,而我只想将自己的CustomCell(来自xib文件)实现到我的情节提要,而我不知道如何连接它来显示我的数据(我已经检查过我的标识符),这是控制器的代码:

class SearchRecipe: UIViewController, ShowAlert {

var recipeData = RecipeDataModel()
var recipe = [String]()


@IBOutlet weak var tableViewSearch: UITableView!

override func viewDidLoad() {
    self.tableViewSearch.rowHeight = 130
}

func updateRecipeData(json: JSON){

    if let ingredients = json["hits"][0]["recipe"]["ingredientLines"].arrayObject{

        recipeData.ingredientsOfRecipe = ingredients[0] as! String
        recipeData.cookingTime = json["hits"][0]["recipe"]["totalTime"].stringValue
        recipeData.recipe = json["hits"][0]["recipe"]["label"].stringValue
        recipeData.recipeImage = json["hits"][0]["recipe"]["image"].stringValue

    }
    else {
        print("Problem")
    }
    //self.tableViewSearch.reloadData()
   }
}
extension SearchRecipe: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return recipe.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "customRecipeCell", for: indexPath) as! CustomRecipeCell
    getRecipesDisplay(in: cell, from: recipeData , at: indexPath)
    return cell
}


func getRecipesDisplay(in cell: CustomRecipeCell, from recipeModel: RecipeDataModel, at indexPath: IndexPath){

    cell.recipeTitle.text = recipeData.recipe
    cell.recipeInfos.text = recipeData.ingredientsOfRecipe
    cell.timerLabel.text = recipeData.cookingTime
  }   
}

这是我的xib文件的代码:

class CustomRecipeCell: UITableViewCell {


@IBOutlet weak var recipeTitle: UILabel!
@IBOutlet weak var recipeInfos: UILabel!
@IBOutlet weak var cellImageBackground: UIImageView!
@IBOutlet weak var likeAndTimerView: UIView!
@IBOutlet weak var likeImage: UIImageView!
@IBOutlet weak var timerImage: UIImageView!
@IBOutlet weak var likeLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

override func awakeFromNib() {
    super.awakeFromNib()
    activityIndicator.isHidden = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
  }
}

1 个答案:

答案 0 :(得分:0)

在ViewDidLoad中,您必须以这种方式注册单元:

override func viewDidLoad() {
  tableViewSearch.register(UINib(nibName:" /* NAME OF YOUR XIB FILE */ ", bundle: nil), forCellReuseIdentifier: "customRecipeCell")
}

您还必须在自定义单元格和表格视图的属性检查器中编辑单元格的大小