简体   繁体   中英

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. 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:

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. not self.textField use 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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