2017-07-02 3 views
0

私は自分のアプリケーションに設定を入力するためのユーザー用のカスタムインターフェイスを構築しています。私はexample I found at AppCodaの後に展開可能な行を使用しています。私は、Swift 3/4を使用し、plistから読み込むのではなく、コードからセル情報を使用するという例を修正しました。スウィフトテーブルで展開可能なセル - セルの再利用/デキュー?

一部のセルコンテンツが画面に表示される方法に問題があります。展開して折りたたむ行には、ユーザー入力が可能なテキストフィールドが含まれています。下のサンプルコードには、このような行が4つあります。

これらのセルのいずれかにエントリが作成されると、4つのセルが展開されたときに最後に入力された値が表示される場合とされない場合があります。 「余分な」テキストはそこに所属する情報を上書きします。

私はこの違反しているテキストを取り除くために私が考えることができるすべてを試しましたが、私は壁に向かって頭を叩いています。私は何が欠けていますか?

FWIW、私は現在、他の場所で同様のソリューションを検討しています。ここで一つは、私はかなり好きです。

https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section

この1つは面白そうだけどスウィフトではありません。

https://github.com/singhson/Expandable-Collapsable-TableView

同じコメント:

https://github.com/OliverLetterer/SLExpandableTableView

このルックス非常に興味深い - よくサポートされている - しかし私は調査する時間がなかった:

https://github.com/Augustyniak/RATreeView

ここに同様の要求:

Expand cell when tapped in Swift

ここで説明し、同様の問題が、私はすでに提案されたものをやっていると思いますか?ここで

http://www.thomashanning.com/the-most-common-mistake-in-using-uitableview/

私のテーブルビューコントローラのコードです。私は問題があると信じています...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath): 

...機能ですが、私の人生のために私はそれを見ることができません。

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    test = defineCellProps() // This loads my hard-coded cell properties into array "test" 
    configureTableView() 
} 

func configureTableView() { 
    loadCellDescriptors() 
    tblExpandable.delegate = self 
    tblExpandable.dataSource = self 
    tblExpandable.tableFooterView = UIView(frame: CGRect.zero) 
    tblExpandable.register(UINib(nibName: "NormalCell", bundle: nil), forCellReuseIdentifier: "idCellNormal") 
    tblExpandable.register(UINib(nibName: "TextfieldCell", bundle: nil), forCellReuseIdentifier: "idCellTextfield") // There are additional cell types that are not shown and not related to the problem 
} 

func loadCellDescriptors() { // Puts the data from the "test" array into the format used in the original example 
    for section in 0..<ACsections.count { 
     var sectionProps = findDict(matchSection: ACsections[section], dictArray: test) 
     cellDescriptors.append(sectionProps) 
    } 
    cellDescriptors.remove(at: 0) // Removes the empty row 
    getIndicesOfVisibleRows()   
    tblExpandable.reloadData() // The table 
} 

func getIndicesOfVisibleRows() { 
    visibleRowsPerSection.removeAll() 
    for currentSectionCells in cellDescriptors { // cellDescriptors is an array of sections, each containing an array of cell dictionaries 
     var visibleRows = [Int]() 
     let rowCount = (currentSectionCells as AnyObject).count as! Int 
     for row in 0..<rowCount { // Each row is a table section, and array of cell dictionaries 
      var testDict = currentSectionCells[row] 
      if testDict["isVisible"] as! Bool == true { 
       visibleRows.append(row) 
      } // Close the IF 
     } // Close row loop 
     visibleRowsPerSection.append(visibleRows) 
    } // Close section loop 
} // end the func 


func getCellDescriptorForIndexPath(_ indexPath: IndexPath) -> [String: AnyObject] { 
    let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row] 
    let cellDescriptor = (cellDescriptors[indexPath.section])[indexOfVisibleRow] 
    return cellDescriptor as [String : AnyObject] 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath) 
    let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell 
    cell.textLabel?.text = nil 
    cell.detailTextLabel?.text = nil 
    cell.textField?.placeholder = nil 
    if currentCellDescriptor["cellIdentifier"] as! String == "idCellNormal" { 
     if let primaryTitle = currentCellDescriptor["primaryTitle"] { 
      cell.textLabel?.text = primaryTitle as? String 
     } 

     if let secondaryTitle = currentCellDescriptor["secondaryTitle"] { 
      cell.detailTextLabel?.text = secondaryTitle as? String 
     } 
    } 
    else if currentCellDescriptor["cellIdentifier"] as! String == "idCellTextfield" { 
     if let primaryTitle = currentCellDescriptor["primaryTitle"] { 
      if primaryTitle as! String == "" { 
       cell.textField.placeholder = currentCellDescriptor["secondaryTitle"] as? String 
       cell.textLabel?.text = nil 

      } else { 
       cell.textField.placeholder = nil 
       cell.textLabel?.text = primaryTitle as? String 
      } 
     } 

     if let secondaryTitle = currentCellDescriptor["secondaryTitle"] { 
      cell.detailTextLabel?.text = "some text" 
     } 
     cell.detailTextLabel?.text = "some text" 
// This next line, when enabled, always puts the correct row number into each cell. 
//   cell.textLabel?.text = "cell number \(indexPath.row)." 
    } 

    cell.delegate = self 

    return cell 
} 

は、ここで私がほとんど変化してCustomCellコードです:

import UIKit 

protocol CustomCellDelegate { 
func textfieldTextWasChanged(_ newText: String, parentCell: CustomCell) 
} 

class CustomCell: UITableViewCell, UITextFieldDelegate { 

@IBOutlet weak var textField: UITextField! 

let bigFont = UIFont(name: "Avenir-Book", size: 17.0) 
let smallFont = UIFont(name: "Avenir-Light", size: 17.0) 
let primaryColor = UIColor.black 
let secondaryColor = UIColor.lightGray 

var delegate: CustomCellDelegate! 

override func awakeFromNib() { 
    super.awakeFromNib()   // Initialization code 

    if textLabel != nil { 
     textLabel?.font = bigFont 
     textLabel?.textColor = primaryColor 
    } 

    if detailTextLabel != nil { 
     detailTextLabel?.font = smallFont 
     detailTextLabel?.textColor = secondaryColor 
    } 

    if textField != nil { 
     textField.font = bigFont 
     textField.delegate = self 
    }   
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    // Configure the view for the selected state 
} 

override func prepareForReuse() { // I added this and it did not help 
    super.prepareForReuse() 
    textLabel?.text = nil 
    detailTextLabel?.text = nil 
    textField?.placeholder = nil 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    if delegate != nil { 
     delegate.textfieldTextWasChanged(textField.text!, parentCell: self) 
    } 
    return true 
    } 
} 

答えて

0

OMGは、私は私の額に自分の手のひらを叩くんです。上のコードから欠けている重要な行が1つあります:

override func prepareForReuse() { 
super.prepareForReuse() 
textLabel?.text = nil 
detailTextLabel?.text = nil 
textField?.placeholder = nil 
} 

あなたは欠けているものが見えますか?

textField?.text = nil 

これはすべてです。私はラベルでうんざりしていましたが、テキストフィールドのテキスト自体ではありませんでした。

関連する問題