Ios 向swift中的UITableViewCell添加文本字段
我尝试在单击按钮时动态添加文本字段 我认为最简单的方法是创建一个初始表视图,然后动态添加包含文本字段的新单元格 但是,当我添加一个新单元格时(文本字段下面显示2个单元格),我无法确定我做错了什么Ios 向swift中的UITableViewCell添加文本字段,ios,swift,uitableview,uitextfield,Ios,Swift,Uitableview,Uitextfield,我尝试在单击按钮时动态添加文本字段 我认为最简单的方法是创建一个初始表视图,然后动态添加包含文本字段的新单元格 但是,当我添加一个新单元格时(文本字段下面显示2个单元格),我无法确定我做错了什么 // create a cell for each table view row func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { print("
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("in delegte table view")
return createTableRow()
}
func createTableRow() -> UITableViewCell{
let cell:UITableViewCell = self.extraGuestsTable.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
cell.textLabel?.text = "some text"
let tf = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 20))
tf.placeholder = "Enter text here"
tf.font = UIFont.systemFont(ofSize: 15)
//cell.contentView.addSubview(tf)
cell.addSubview(tf)
return cell
}
如何使文本字段显示在正确的表格视图单元格中?
谢谢
编辑
多谢大家——正如一些人指出的,问题在于y位置被设置为100——因此显示在单元格下方。
应该是:
let tf = UITextField(frame: CGRect(x: 20, y: 0, width: 300, height: 20))
文本字段框的大小应为单元格大小,并检查数据源 使用 而不是
cell.addSubview(tf)
默认高度为44,如果需要更多,请执行相应的代理
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
最后添加代码
class customCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后更改您的代码:
let cell: customCell = self.extraGuestsTable.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as customCell!
let tf = UITextField(frame: CGRect(x: 20, y: 0, width: 300, height: 20))
tf.placeholder = "Enter text here"
tf.font = UIFont.systemFont(ofSize: 15)
//cell.contentView.addSubview(tf)
cell.addSubview(tf)
return cell
文本字段框的大小应为单元格大小,并检查数据源 使用 而不是
cell.addSubview(tf)
默认高度为44,如果需要更多,请执行相应的代理
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
最后添加代码
class customCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后更改您的代码:
let cell: customCell = self.extraGuestsTable.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as customCell!
let tf = UITextField(frame: CGRect(x: 20, y: 0, width: 300, height: 20))
tf.placeholder = "Enter text here"
tf.font = UIFont.systemFont(ofSize: 15)
//cell.contentView.addSubview(tf)
cell.addSubview(tf)
return cell
我已经试过了,它现在被评论掉了,但是它不能解决问题,你的手机高度是多少?你给y位置100?创建自定义单元格并使用它。实际上我还没有设置单元格高度-因为单元格是动态的-上面的代码就是全部,故事板中没有任何内容。另外,数据源只是一个空数组:var tableData=[String]()在数据源中返回一些计数,并选中Use custom cellI我已经尝试过了,它当前已被注释掉,但它无法修复它您的单元格高度是多少?你给y位置100?创建自定义单元格并使用它。实际上我还没有设置单元格高度-因为单元格是动态的-上面的代码就是全部,故事板中没有任何内容。此外,数据源只是一个空数组:var tableData=[String]()在数据源中返回一些计数,并选中Use custom cell