如何将一个按钮添加到单元格末尾Swift
答
我以为你知道在UITableView上创建原型单元格。根据上面的问题,在现有的tableView上还需要一个原型单元格。在下面的代码中,UITableView
有两个prototypeCell
。
尝试下面的代码,
import UIKit
class TableViewController: UIViewController,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var greetings = ["Vanakkam","Hello","Hi","Ni Hao","Namastae","Selamat Tinga","Oi"]
override func viewDidLoad(){
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return greetings.count+1 // +1 for button
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < greetings.count{ // (0...N).count = N+1. Already last row added at numberOfRowsInSection
let normalCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")
normalCell?.textLabel?.text = greetings[indexPath.row] //
normalCell?.textLabel?.textAlignment = .center
return normalCell!
}else{ // last row
let buttonCell = self.tableView.dequeueReusableCell(withIdentifier: "cellButton")
return buttonCell!
}
}
}
输出:
你是什么意思A CEL l 10行?一个单元应该是一个行。你的意思是你有十个电池吗? –
让我们只是说我想在单元格的末尾添加一个按钮。这就是全部 – Jack
@Jack,如果按单元格表示单元格,那么你可以创建一个自定义原型单元格,并在那里放置一个按钮并使用约束来对齐。但是你的问题很模糊,所以我建议你指定它。 – Olter