swift - UITableView.insertRowsAtIndexPaths not working -
i'm using core data , have problem. app crashes because when insert new element try access cell doesn't exist should.
the code matters:
func controllerwillchangecontent(controller: nsfetchedresultscontroller) { tableview.beginupdates() } func controllerdidchangecontent(controller: nsfetchedresultscontroller) { tableview.endupdates() } func controller(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) { switch type { case .insert: if let indexpath = newindexpath { self.tableview.insertrowsatindexpaths([indexpath], withrowanimation: .fade) // doesn't work print(indexpath) // returns row 0 section 0 print(tableview.numberofrowsinsection(0)) // returns 0 print(tableview.cellforrowatindexpath(indexpath)) // returns nil let cell = tableview.cellforrowatindexpath(indexpath) as! mycell // app crashes here because cell nil configurecell(cell, atindexpath: indexpath) self.tableview.backgroundview = nil }
crash log:
<uitableview: 0x7ffb1b8c3000; frame = (0 64; 375 554); clipstobounds = yes; autoresize = w+h; gesturerecognizers = <nsarray: 0x7ffb1ae9a620>; layer = <calayer: 0x7ffb1ae92b40>; contentoffset: {0, 0}; contentsize: {375, 10}> <nsindexpath: 0xc000000000000016> {length = 2, path = 0 - 0} 0 nil fatal error: unexpectedly found nil while unwrapping optional value
sorry putting log code there's bug block quote , won't show full log.
this weird because working fine before. did changes code (code snippet includes changes) didn't touch insertrowsatindexpaths
, it's not working.
it's normal ! indeed, cellforrowatindexpath
on uitableview
"returns object representing cell of table, or nil if cell not visible or indexpath out of range". (source).
so, can't access cell because not exists yet uitableview
(you adding !). please, move configurecell
call datasource tableview(_:cellforrowat:)
.
Comments
Post a Comment