简体   繁体   English

如何用内容填充分组的动态Table View单元格(快速)

[英]How to fill grouped dynamic Table View cells with content (swift)

I'm struggling with grouped dynamic table view. 我正在为分组动态表视图苦苦挣扎。 I want to have 2 groups. 我想有2个小组。 Basically, in first group I want to have customers data and on second one orders. 基本上,在第一组中,我要获得客户数据,并在第二个订单中。 First group will have always same number of rows. 第一组将始终具有相同的行数。 Any what is my problem - I want to put text field into each row of first section (with different placeholder) but when I run application, textfield is only in last row. 有什么问题吗-我想将文本字段放入第一部分的每一行(使用不同的占位符),但是当我运行应用程序时,文本字段仅位于最后一行。 Does anybody know what Im doing wrong? 有人知道我在做什么错吗? Thank you 谢谢

Declaration of my properties 声明我的财产

var textField = UITextField()
var udaje = ["Jméno zákazníka","Kontaktní osoba","Telefon","Email","Fakturační adresa","Dodací adresa","IČO/DIČ"]

TableViewController.swift: TableViewController.swift:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = UITableViewCell()

if (indexPath.section == 0) {
    let cell = tableView.dequeueReusableCellWithIdentifier("udajeCell", forIndexPath: indexPath) as! UITableViewCell

    self.textField.frame = cell.contentView.frame
    self.textField.placeholder = self.udaje[indexPath.row]

    cell.contentView.addSubview(textField)
} else if (indexPath.section == 1) {
    let cell = tableView.dequeueReusableCellWithIdentifier("pilyCell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = "Přidat pilu"
    cell.detailTextLabel?.text = "+"
}

return cell
}

在此处输入图片说明

try this way code: 试试这种方式的代码:

your problem is use use the self.textField. 您的问题是使用self.textField。 not self.textField use cell.textField. 不要self.textField使用cell.textField。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = UITableViewCell()

if (indexPath.section == 0) {
    let cell = tableView.dequeueReusableCellWithIdentifier("udajeCell", forIndexPath: indexPath) as! UITableViewCell

    cell.textField.frame = cell.contentView.frame
    cell.textField.placeholder = self.udaje[indexPath.row]

    cell.contentView.addSubview(textField)
} else if (indexPath.section == 1) {
    let cell = tableView.dequeueReusableCellWithIdentifier("pilyCell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = "Přidat pilu"
    cell.detailTextLabel?.text = "+"
}

return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM