gpt4 book ai didi

ios - TableView 滚动错误

转载 作者:行者123 更新时间:2023-11-28 15:42:49 26 4
gpt4 key购买 nike

我刚刚开始学习在 Xcode 上编写代码,目前正在学习如何使用 TableView 功能。我正在尝试创建 50 个包含数字 1 到 50 的单元格,但是当我在 iPhone 7 上进行模拟时,我只能向下滚动到 15。第 15 个单元格也不是完整的单元格,当我水平旋转我的设备时我只能看到 8 个单元格。在这方面需要帮助:(

这是我正在使用的代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 50

}

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

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

cell.textLabel?.text = String(indexPath.row + 1)

return cell

}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

这是屏幕底部的屏幕截图: Even cell 15 is not showing the full height

最佳答案

更改您的代码如下::

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

var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell")
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
}
cell.textLabel?.text = "\(indexPath.row + 1)."

return cell

}

关于ios - TableView 滚动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43519481/

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