2017-09-16 27 views
0

拡張可能なヘッダービューを持つことができるUITableViewを作成しようとしています。あなたがヘッダビューの内側にボタンを押すと、以下の機能が実行されます:UITableViewのセル挿入エラー

func expandTheCell(_ sender: UIButton) { 
    self.tableView.beginUpdates() 

    if sender.isExpanded == false { 

     postsArray.append("Hello") 
     tableView.reloadData() 
     print("Array Count: \(postsArray.count)") 
     self.tableView.insertRows(at: [IndexPath.init(row: 0, section: sender.tag)], with: .fade) 

    } else { 
     print("test") 
    } 

    self.tableView.endUpdates() 
} 

これは、いくつかのテーブルビュー機能です:私は行を挿入しようとすると

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

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

は、私は次の取得しますエラー:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1

どのようにしてセルを挿入できませんか?私は間違って何をしていますか?

+0

'tableView.reloadData()'を削除してから、プログラムを実行してください。 – 3stud1ant3

+0

まだ削除されていません。 –

+0

も ​​'postsArray.append(" Hello ")'と書いておきます。エラーも? – 3stud1ant3

答えて

0

あなたが挿入した後、私は、問題はあなたがボタンをクリックする上postsArrayに項目を追加する場合、同じpostsArrayは、他のセクションのために使用され、postsArrayあなたのケースでは、セクションで同じのdataSource配列を有することに起因していると思います行section 0で、section 1前と挿入操作の後に私のための行の数を文句を言うことは同じではありませんが、section 0 doesntのは、それが今、この問題は二つに解決することができるpostsArray

で同じ行数と項目数を持っているので、文句を言います方法:

最初の方法はpostsArray

第二の方法の要素の数は、すべてのセクションに対して異なるデータソースのアレイを有することであるように、すべてのセクションが行の同じ数を有している、あなたは、他のセクションの行を挿入することができるということですセクション1の場合はpostsArray1、セクション2の場合はpostsArray2、その他のセクションの場合は同じです。今度は、他のセクションの行を挿入する必要はありません。各セクションには異なるデータソース配列があり、変更すると他のセクションに影響はありません。このことができます

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
     @IBOutlet weak var tableView: UITableView! 

     override func viewDidLoad() { 
       super.viewDidLoad() 

       let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(buttonTapped(_:))) 
       self.navigationItem.rightBarButtonItem = addButton 
     } 


     var valuesFirstSection = ["value1", "value2", "value3"] 
     var valuesSecondSection = ["value1Second", "value2Second", "value3Second"] 

     //if you want to have the same dataSource array then use this 

     //var sharedValues = ["value1Shared", "value2Shared", "value3Shared"] // shared dataSource array 


     func numberOfSections(in tableView: UITableView) -> Int { 
       return 2 
     } 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

       if section == 0 { 
         return valuesFirstSection.count 
       }else { 
         return valuesSecondSection.count 
       } 
//    //if you want to have the same dataSource array then 
       //use this 

       //return sharedValues.count; 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

       if indexPath.section == 0 { 
         cell.textLabel?.text = valuesFirstSection[indexPath.row] 


       }else { 

         cell.textLabel?.text = valuesSecondSection[indexPath.row] 

       } 
       return cell 

       //if you want to have the same dataSource array then 
       //use this 

       //cell.textLabel?.text = sharedValues[indexPath.row] 
       //return cell 

     } 

     func buttonTapped(_ sender: UIBarButtonItem) { 



       //if you want to have the same dataSource array then 
       //you need to insert the rows for other sections as well 
//    sharedValues.insert("NewValue0", at: 0) 
//    self.tableView.insertRows(
//      at: [IndexPath(row: 0, section: 0), 
//        IndexPath(row: 0, section: 1) 
//      ], 
//      with: .automatic) 


       valuesFirstSection.insert("NewValue0", at: 0) 
       self.tableView.insertRows(
         at: [IndexPath(row: 0, section: 0) 
         ], 
         with: .automatic) 


     } 



} 

希望:

私は上記の理論を実証するための簡単なプロジェクトを行っています。

+0

うん、それはうまくいった。私のアプリケーションで今この問題は、セクションの量がユーザーに依存するため、私はすべてのセクションの配列を事前定義することができなくなるということです。私はセクションの量を知らない場合でもそれを動作させる方法はありますか? –

+0

@ Mr.Man「セクションの量はユーザーに依存します」もっと説明してください – 3stud1ant3

+0

このテーブルビューは、他の人の投稿を見るためのものです。たとえば、多くの人に従わない場合、User1にはセクションが1つしかない場合があります。多くの人に従う場合、User2には50のセクションがあるかもしれません。 –

関連する問題