我在我的 viewDidLoad()
中尝试此操作:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 300.0
}
return 100.0
}
但这没有效果
在swift 3中以以下方式写代码行的高度是tableView委托方法,所以您要这样做。不在ViewDidload方法内写入,您可以在ViewDidload方法的外部写入
导入Uikit
class ProfileViewController: UIViewController,ProfileViewControllerDelegate {
//MARK: - Outlet -
@IBOutlet weak var tblView: UITableView!
//MARK: - View Life Cycle -
override func viewDidLoad() {
}
//MARK: TablView Delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 300.0
}
return 100.0
}
这是简单表格的示例,第一行高度为300和其他100。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "CellID") as UITableViewCell!
cell.textLabel?.text = "testing"
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number (indexPath.row).")
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 300.0
}
return 100.0
}
}