gpt4 book ai didi

ios - Swift Table View Cell 布局折叠所有元素

转载 作者:行者123 更新时间:2023-11-30 11:31:55 25 4
gpt4 key购买 nike

我对 Swift 和 iOS 编程相当陌生,但我面临以下问题。我正在开发 TableViewController,并在第一个单元格上开始设计布局。我创建了一个所有边距为 0、高度为 150 的 View ,然后我将所有组件排列在水平堆栈 View 内,并添加了我认为合适的约束。在 Storyboard上,一切看起来都很好,但是当我在模拟器上运行应用程序时,所有元素都会崩溃并且不保持其比例。这是 Storyboard的屏幕截图。

ViewCell in Storyboard

据我所知,ContentView 和 tableCell 似乎没有设置约束的方法,我尝试设置自定义行高或指定某些元素的高度和宽度,但它仍然崩溃。我可能做错了什么?

这是它在模拟器上的样子:

emulator screencaupture

编辑添加代码这是 TableView Controller 的代码

class UserPostsTableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(RegisterPost(_:)))

self.tableView.estimatedRowHeight = 150.0

self.tableView.rowHeight = UITableViewAutomaticDimension

let sv = UIViewController.displaySpinner(onView: self.view)

let userName = userPreferences.object(forKey: "sessionEmail") as! String

apiClient.GetPostsByUser(userName : userName) { response in
self.posts = response
self.tableView.reloadData()
UIViewController.removeSpinner(spinner: sv)
}
}

let apiClient = ApiClient()

let userPreferences = UserDefaults.standard

var posts = [Post]()

@objc func RegisterPost(_ sender : UIButton){
self.performSegue(withIdentifier: "registroPostSegue", sender: nil)
}

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 {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return posts.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postIdentifier", for: indexPath) as! PostCellTableViewCell

let post = posts[indexPath.row]

cell.postLabel.text = post.nombre
cell.categoryLabel.text = post.categoria
cell.postImageView.kf.setImage(with: URL(string: post.imagen!))

cell.postIsOpen(isOpen: post.abierto)

return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 150.0
}

}

编辑2捕获该 Controller 上的元素分布

enter image description here

最佳答案

尝试在代码中使用以下委托(delegate)方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150.0
}

编辑:快速:

func tableView(_ tableView: UITableView, 
heightForRowAt indexPath: IndexPath) -> CGFloat{
return 150.0
}

关于ios - Swift Table View Cell 布局折叠所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50168238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com