【发布时间】:2020-06-29 07:18:51
【问题描述】:
我正在尝试添加一个选项以在表中添加其他学生字段,以便用户可以添加多个学生姓名。
但是我很困惑如何使用表格视图来做到这一点。
我对隐藏具有特定数量字段的视图不感兴趣。
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
struct listItems{
var title : String
var isExpandable:Bool
var maxFields :Int
init(title:String,isExp:Bool,mxF:Int) {
self.title = title
self.isExpandable = isExp
self.maxFields = mxF
}
}
@IBOutlet weak var tblListTable: UITableView!
let data : [listItems] = [listItems(title: "Name", isExp: false, mxF: 1), listItems(title: "Student Name", isExp: true, mxF: 20), listItems(title: "Email", isExp: false, mxF: 1)]
override func viewDidLoad() {
super.viewDidLoad()
tblListTable.delegate = self
tblListTable.dataSource = self
self.tblListTable.reloadData()
print("isLoaded")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForRow")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ListCell
cell.lblName.text = data[indexPath.row].title
if data[indexPath.row].isExpandable == true {
cell.btnAddField.isHidden = false
print("ishidden")
}
else {
cell.btnAddField.isHidden = true
}
return cell
}
}
列出单元格类
import UIKit
protocol AddFieldDelegate : class {
func addField( _ tag : Int)
}
class ListCell: UITableViewCell {
@IBOutlet weak var btnAddField: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var txtField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func addField( _ tag : Int){
}
}
【问题讨论】:
-
告诉我们你到目前为止做了什么。
-
好的.. 我会编辑更多细节的问题
-
@Jaseel.Dev 您需要根据数据数组中所需的索引添加另一个对象。例如:
listItems(title: "NickName", isExp: false, mxF: 1)它应该插入到数据数组的索引 2 处。然后重新加载表格视图。我希望你能澄清我的观点。 -
是的..你能不能用代码把它解释为答案..我会把它标记为答案
标签: swift xcode swift3 swift4 swift5