2016-05-09 12 views
0

私はストーリーボードを使用して2つのセクションでテーブルビューを作成しました。 配列の配列を使ってセクションごとに異なるアイテムを表示し、それぞれの行を個別に削除できるようにします。 私はこれが私のtableViewControllerある複数のセクションを含むテーブルビューをスウィフト

import Foundation 
import UIKit 
var letters: [String] = [] 
class NotificationsViewController: UITableViewController { 




    var data = [ letters, ["1","2","3"]] 



    override func viewDidLoad() { 



     super.viewDidLoad() 
     self.tableView.editing = true 

    } 


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //return letters.count 
    return data[section].count 

    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "tableCell") 

     // cell.textLabel?.text = letters[indexPath.row] 
      cell.textLabel?.text = data[indexPath.section][indexPath.row] 

     return cell 
    } 



    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == .Delete { 
//   // Delete the row from the data source 
//   letters.removeAtIndex(indexPath.row) 
//   
//   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

      data[indexPath.section].removeAtIndex(indexPath.row) 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 



     } 
    } 


} 

次のことを試してみましたが、アプリのクラッシュ:アプリケーション48.278 [70893:6874230] screeshot

エラー:

2016年5月10日17:01 * 'NSRangeException'、理由: '* - [__ NSArrayI objectAtIndex:]:境界1を超えたインデックス1' ***ファーストスローコールスタック:

私は、文字列として1つの配列を持っており、それはあなたが文字を削除し、次のようにデータを交換してくださいすることができます両方のセクション

+0

問題を理解するために十分な情報を提供する必要があります。アプリがクラッシュしたときに表示されるエラーは何ですか?これはあなたのTableViewControllerのための完全なソースですか? –

+0

はい、これは完全なsource.Errorです:2016-05-10 02:54:39.757 App [67795:6715726] ***キャッチされていない例外 'NSRangeException'の理由によるアプリケーションの終了、理由: '*** - [__ NSArrayI objectAtIndex :]:範囲を超えたインデックス1 [0 .. 0] ' ***ファーストスローコールスタック: – Jana

答えて

0

で同じデータを表示する際にアプリが正常に動作します:

var data = [["1","2","3"]] 

とあれば次のような複数のセクションが必要です。

var data = [["1","2","3"], ["4","5"]] 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return data.count 
} 

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 20 
} 
0

私の問題が見つかりました。 テーブルビューのセクションごとに1つの行があるからです。一度追加した行は、アプリが動作し、クラッシュしませんでした。

関連する問題