2016-11-05 9 views
0

私はUITableViewを持っています。これは2つのカスタムプロトタイプセルを持っています.1つのセルは固定されているだけで、テーブルビューには残りますが、別のセルがテーブルビューに再び追加されます&以下ボタンクリックでUITableViewでセルを追加する方法は?

は、テーブルビューのメソッドのコードです:

func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return arr_fields.count 
} 

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

    if indexPath.section == 0 { 
     let cellIndentifier="property" 

     let cell:CreatePropertyCell=tableView.dequeueReusableCell(withIdentifier: cellIndentifier, for: indexPath) as! CreatePropertyCell 
     cell.layer.borderWidth = 2.0 
     cell.layer.borderColor = UIColor.gray.cgColor 
     cell.txt_propertyName.attributedPlaceholder = NSAttributedString(string:"Name of property", 
                     attributes:[NSForegroundColorAttributeName: UIColor.gray]) 

     return cell 

    } else { 
     let cellIdentifier = "attribute" 
     let cell:AddProperty=tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! AddProperty 
     cell.layer.borderWidth = 2.0 
     cell.layer.borderColor = UIColor.gray.cgColor 
     cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute", 
                     attributes:[NSForegroundColorAttributeName: UIColor.gray]) 
     cell.btnAdd.tag=indexPath.row 
     return cell 

    } 
} 

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 15 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let header = UIView() 
    header.backgroundColor = UIColor.clear 
    return header 

} 

@IBAction func actionAddProperty(_ sender: AnyObject) { 
    let cellIdentifier = "attribute" 
    let cell:AddProperty = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! AddProperty 
    cell.layer.borderWidth = 2.0 
    cell.layer.borderColor = UIColor.gray.cgColor 
    cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute", 
                    attributes:[NSForegroundColorAttributeName: UIColor.gray]) 
    cell.tag=sender.tag!+1 
    arr_fields.append(cell) 
    tableView.reloadData() 
    print("tag is \(sender.tag!)") 
} 

は、今私はちょうど今のないcells.Asそれがテーブルビューに細胞の両方を追加し、両方のテーブルビューに2つ目のセルを追加したいです。

+0

あなたはarr_fields'配列 '内の全セルを格納している理由は?どのようにこの配列を宣言しましたか?そのタイプは何ですか? –

+0

Welll私はテキストフィールドを持っており、ボタンを追加するボタンをクリックすると、現在のcell.Soの下に新しいテキストフィールドを追加したいのですが、ボタンをクリックして新しいセルを作成し、配列に追加してデータをリロードすると思います。 – Techiee

+0

arr_field配列のデータ型は? –

答えて

0

このお試しください:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if section == 0 { 
     return 1 
    } else { 
     return arr_fields.count 
    } 
} 

挿入セル:むしろ表をリロードするよりは、表の中に一つのセルを挿入します。

@IBAction func actionAddProperty(_ sender: AnyObject) { 

    let cellIdentifier = "attribute" 
    let cell:AddProperty = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! AddProperty 
    cell.layer.borderWidth = 2.0 
    cell.layer.borderColor = UIColor.gray.cgColor 
    cell.txt_AddProperty.attributedPlaceholder = NSAttributedString(string:"Property Attribute", 
                    attributes:[NSForegroundColorAttributeName: UIColor.gray]) 
    cell.tag=sender.tag!+1 

    let indexPath = NSIndexPath.init(forRow: arr_fields.count-1, inSection: 1) 
    tableView.beginUpdates() 
    arr_fields.append(objComment) 
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    tableView.endUpdates() 

} 
+0

私はswiftを使用しています。 – Techiee

+0

セクションの行の引数ラベルとしてエラーが発生しました。tableviewのセクションの数はどのくらいですか?(例:forrow:arr_fields.count-1、inSection:1) – Techiee

+0

NSIndexPathの代わりにIndexPathを使用できました –

関連する問題