如何在 table 视图 swift 中的某些数据后添加另一个部分(按钮)?
How to add another section (button) after some data in table view swift?
我正在尝试根据用户输入创建多个部分。
我能够做到这一点,但下一步是在该部分的末尾创建按钮。
但是,我们不知道最后一部分是什么,因为它取决于用户请求。
它看起来像这样:
现在,我已经为按钮创建了另一个单元格,如下所示:
到目前为止,这是我的代码:
var numberOfSection: Int = 0 {
didSet {
tableView.reloadData()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
if numberOfSection == 0 {
return 2
}
else if numberOfSection <= numberOfSection {
return numberOfSection + 1
}
return numberOfSection + 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
else if section <= numberOfSection + 1 {
for _ in 0...numberOfSection {
return testeSpec.count
}
}
else {
return 1
}
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = indexPath.section
if tableSection == 0 {
let cells = tableView.dequeueReusableCell(withIdentifier: "serverNumbCell", for: indexPath) as! CellOutlet
let txtFieldServerNumber = cells.txtFieldServerNumb
cells.lblSectionNumb.text = "Number of section:"
txtFieldServerNumber?.addTarget(self, action: #selector(sectionTxtFieldDidChange), for: .editingChanged)
return cells
}
else if tableSection <= numberOfSection + 1 {
for _ in 0...numberOfSection {
let cells = tableView.dequeueReusableCell(withIdentifier: "serverSpecCell", for: indexPath) as! CellOutlet
let specWS = testSpec[indexPath.row]
cells.labels.text = specWS.spec
return cells
}
}
else {
let cells = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! CellOutlet
cells.btnResult.setTitle("OK", for: .normal)
return cells
}
return CellOutlet()
}
我还有一个 class 连接了所有的插座。
在所有这些未知数量的部分之后,如何在一个部分中创建另一个按钮?
提前致谢
在ViewControllerclass
class ViewController: UIViewController {
var numberOfSections: Int = 0
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return numberOfSections + 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
"section"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if numberOfSections > section {
return 2
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
cell.titleLabel.text = "Number of section"
cell.numberOfSectionAction = { userInput in
self.numberOfSections = userInput
self.tableView.reloadData()
}
return cell
}
if numberOfSections > indexPath.section {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
return cell
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell") as? ButtonCell else {return UITableViewCell()}
return cell
}
}
class ButtonCell: UITableViewCell {
override class func awakeFromNib() {
super.awakeFromNib()
}
}
在 UITableViewCell 子class
class ServerSpecCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var serverTextField: UITextField!
var numberOfSectionAction: ((Int)->Void)?
override func awakeFromNib() {
super.awakeFromNib()
serverTextField.delegate = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func textFieldDidChangeSelection(_ textField: UITextField) {
guard let sections = Int(textField.text ?? "0") else { return }
numberOfSectionAction?(sections+1)
}
}
我正在尝试根据用户输入创建多个部分。 我能够做到这一点,但下一步是在该部分的末尾创建按钮。 但是,我们不知道最后一部分是什么,因为它取决于用户请求。 它看起来像这样:
现在,我已经为按钮创建了另一个单元格,如下所示:
到目前为止,这是我的代码:
var numberOfSection: Int = 0 {
didSet {
tableView.reloadData()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
if numberOfSection == 0 {
return 2
}
else if numberOfSection <= numberOfSection {
return numberOfSection + 1
}
return numberOfSection + 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
else if section <= numberOfSection + 1 {
for _ in 0...numberOfSection {
return testeSpec.count
}
}
else {
return 1
}
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = indexPath.section
if tableSection == 0 {
let cells = tableView.dequeueReusableCell(withIdentifier: "serverNumbCell", for: indexPath) as! CellOutlet
let txtFieldServerNumber = cells.txtFieldServerNumb
cells.lblSectionNumb.text = "Number of section:"
txtFieldServerNumber?.addTarget(self, action: #selector(sectionTxtFieldDidChange), for: .editingChanged)
return cells
}
else if tableSection <= numberOfSection + 1 {
for _ in 0...numberOfSection {
let cells = tableView.dequeueReusableCell(withIdentifier: "serverSpecCell", for: indexPath) as! CellOutlet
let specWS = testSpec[indexPath.row]
cells.labels.text = specWS.spec
return cells
}
}
else {
let cells = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! CellOutlet
cells.btnResult.setTitle("OK", for: .normal)
return cells
}
return CellOutlet()
}
我还有一个 class 连接了所有的插座。 在所有这些未知数量的部分之后,如何在一个部分中创建另一个按钮?
提前致谢
在ViewControllerclass
class ViewController: UIViewController {
var numberOfSections: Int = 0
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return numberOfSections + 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
"section"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if numberOfSections > section {
return 2
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
cell.titleLabel.text = "Number of section"
cell.numberOfSectionAction = { userInput in
self.numberOfSections = userInput
self.tableView.reloadData()
}
return cell
}
if numberOfSections > indexPath.section {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
return cell
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell") as? ButtonCell else {return UITableViewCell()}
return cell
}
}
class ButtonCell: UITableViewCell {
override class func awakeFromNib() {
super.awakeFromNib()
}
}
在 UITableViewCell 子class
class ServerSpecCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var serverTextField: UITextField!
var numberOfSectionAction: ((Int)->Void)?
override func awakeFromNib() {
super.awakeFromNib()
serverTextField.delegate = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func textFieldDidChangeSelection(_ textField: UITextField) {
guard let sections = Int(textField.text ?? "0") else { return }
numberOfSectionAction?(sections+1)
}
}