ios - Inserting rows on a static UITableView -
i'm trying add row static table, i'm getting error:
2015-05-31 13:53:13.153 notify[30565:6091085] *** terminating app due uncaught exception 'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 2 beyond bounds [0 .. 1]'
i have @ibaction (connected uinavigationitem) add row:
@ibaction func addrow(sender: anyobject) { let indexpath = nsindexpath(forrow: rowcount, insection: 0) rowcount += 1 rowadded = true tableview.insertrowsatindexpaths([indexpath], withrowanimation: .fade) }
with attributes rowcount (default in ib design), , bool work out if next row has been added yet:
class notifytableviewcontroller: uitableviewcontroller { var rowcount = 2 var rowadded = false ... }
the table has 1 section, , numberofrowsinsection:
method i've added:
override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { if rowadded { return 3 }else { return 2 } }
and finally, cellforrowatindexpath:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { if indexpath.row == 2 { var cell: uitableviewcell! = tableview.dequeuereusablecellwithidentifier("customcell") as? uitableviewcell if cell == nil { cell = uitableviewcell(style: .default, reuseidentifier: "customcell") cell.textlabel!.text = "new cell" } return cell }else { return super.tableview(tableview, cellforrowatindexpath: indexpath) } }
i've tried wrapping insertrowsatindexpaths call in begin/endupdates, same error. idea problem is?
ok, add rows static table needed implement intentationlevelforrowatindexpath:
, heightforrowatindexpath:
override func tableview(tableview: uitableview, indentationlevelforrowatindexpath indexpath: nsindexpath) -> int { if indexpath.row == 2 { let path = nsindexpath(forrow: 0, insection: indexpath.section) return super.tableview(tableview, indentationlevelforrowatindexpath: path) } return super.tableview(tableview, indentationlevelforrowatindexpath: indexpath) } override func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { if indexpath.row == 2 { return 42 } return super.tableview(tableview, heightforrowatindexpath: indexpath) }
Comments
Post a Comment