我是Xcode的初学者,我在表视图中需要一些关于viewcell的帮助。
这是我的viewcell的属性:
当我试图运行模拟器时,它会变成这样:
我的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创建了一些视图并且它已正常工作。
答案 0 :(得分:1)
调整你的cakeViewController中的单元格高度调用
var a = {id: "1", value: "hello"}
var b = {id: "2", value: "bye"}
var c = {};
[a, b].forEach(function(x){
c[x.id] = x.value;
});
console.log(c);