swift - SIGABRT when selecting rows in TableViewController -


i have problem:
when select 2nd cell of tableview, sigabrt. when select first one, works fine.

must have tableviewcontroller class, because "set rows in section" (from print statement) printed 3 times, when select 2nd cell, 3 * 4 = 12 times, when select first one...
oh... , error:

gonna set rows in section 2 set section 2 1 gonna set rows in section 0 set section 0 1 gonna set rows in section 1 set section 1 3 2016-07-04 12:43:14.407 sessions[1229:629535] *** terminating app due uncaught exception 'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 2 beyond bounds [0 .. 1]' *** first throw call stack: (0x1822e6db0 0x18194bf80 0x1821c7098 0x187b7d5ac 0x187933f1c 0x18756d444 0x18752aff8 0x1878e4b88 0x187529fc4 0x187529d5c 0x187529b68 0x187794a20 0x18752fac8 0x18743fac8 0x187465350 0x18743ed40 0x182c18cc0 0x18743ebc4 0x18744c678 0x18763edbc 0x18743fac8 0x18743f7ac 0x18743ed40 0x182c18cc0 0x18743ebc4 0x18744c678 0x1876df8c0 0x1874535b4 0x18763e8d4 0x18775e334 0x18763e44c 0x1875f8818 0x187503e40 0x187503b1c 0x187503a84 0x1874401e4 0x184dd2994 0x184dcd5d0 0x184dcd490 0x184dccac0 0x184dcc820 0x1874365f8 0x18229c728 0x18229a4cc 0x18229a8fc 0x1821c4c50 0x183aac088 0x1874ae088 0x1000f8b54 0x181d628b8) libc++abi.dylib: terminating uncaught exception of type nsexception 

"set section 2 1" means, section 2 has 1 cell...
class:

import uikit  class sessionstableviewcontroller: uitableviewcontroller {      let header = ["mediatoren", "medianden", "konfliktverlauf"]     var medianden: array<string> = []     var mediators: array<string> = []  //  ---------------- //  mark : ibactions //  ----------------      (i deleted these...)  //  --------------------- //  mark : override funcs //  ---------------------      override func viewdidload() {super.viewdidload() }     override func didreceivememorywarning(){ super.didreceivememorywarning()}     override func viewwillappear(animated: bool) {         print(sessions.workingonsession)         medianden.append((sessions.workingonsession["name1"] as? string)!)         medianden.append((sessions.workingonsession["name2"] as? string)!)         medianden.append((sessions.workingonsession["name3"] as? string)!)         medianden.append((sessions.workingonsession["name4"] as? string)!)         //print(medianden)         _ in 0...3 {             if medianden.last == "" {                 medianden.removelast()             }         }         print(medianden)         mediators.append((sessions.workingonsession["mid"] as? string)!)         mediators.append((sessions.workingonsession["mid2"] as? string)!)         //print(mediators)         _ in 0...1 {             if mediators.last == "" {                 mediators.removelast()             }         }         print(mediators)     }  //  --------------------- //  mark : funcs //  ---------------------      (deleted these too...)  //  ----------------- //  mark : table view //  -----------------      override func tableview(tableview: uitableview, titleforheaderinsection section: int) -> string? {         return self.header[section]     }     override func numberofsectionsintableview(tableview: uitableview) -> int {         return 3     }     override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         print("set rows in section")         print(section)         if section == 0 {             print("set section 0 \(mediators.count)")             return mediators.count         } else if section == 1 {             print("set section 1 \(medianden.count)")             return medianden.count         } else if section == 2 {             print("set section 2 1")             return 1         }         return 0     }     override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {          print("make cells...")         let cell = uitableviewcell()         if indexpath.section == 0 {             cell.detailtextlabel?.text = mediators[indexpath.row]             return cell         } else if indexpath.section == 1 {             cell.textlabel!.text = medianden[indexpath.row]             return cell         } else if indexpath.section == 2 {             let textcell = fstextviewtableviewcell()             return textcell             //textcell.textview.text = sessions.workingonsession["description"] as? string         }         return cell     } } 

thanks everyone!!! :-) , sorry bad english...
fabian

the error message pretty clear.

uncaught exception 'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 2 beyond bounds [0 .. 1]' 

so have array elements [0..1] inclusive , you're accessing element [2]. there many arrays in program: mediators, medianden, self.header. try doing more debugging see 1 @ fault.

it seems assuming mediators has 2 elements , mediaden has 4, , hard-coded. it's better not sprinkle these dependencies throughout code; instead, set size once, , make remainder of code work independently of size (with loops etc).


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -