2015-06-09 3 views
0

У меня есть вычисляемое свойство как переменный экземпляр:Удаления строки в UITableView вызывает NSInternalInconsistencyException с вычисленным свойством

var favorites: [PFObject] 
    get { return Array(ObjectManager.favorites) } 
} 

, где ObjectManager.favorites является Set статическими переменным на ObjectManager класса. Ниже приведены мои методы представления в виде таблицы:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

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

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 

     //delete it from the original set 
     ObjectManager.removeFavoriteObject(favorites[indexPath.row]) 

     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 


     println(favorites.count) 
     // Delete the row from the data source 

    } 

    //If there are no longer any fav objects, then there is no need to display the Edit button. 
    if favorites.isEmpty { 
     self.navigationItem.rightBarButtonItem = nil 
    } 
} 

Это приводит к следующему исключением является триггером, когда я звоню deleteRowsAtIndexPaths.

2015-06-08 21:00:35.320 MedConf[56258:707923] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1623 
2015-06-08 21:00:35.396 MedConf[56258:707923] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001114aec65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000110defbb7 objc_exception_throw + 45 
    2 CoreFoundation      0x00000001114aeaca +[NSException raise:format:arguments:] + 106 
    3 Foundation       0x0000000110a0498f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 
    4 UIKit        0x0000000111b30c53 -[UITableView _endCellAnimationsWithContext:] + 12678 
    5 UIKit        0x0000000121f59f1b -[UITableViewAccessibility deleteRowsAtIndexPaths:withRowAnimation:] + 48 
    6 MedConf        0x000000010f1cebb3 _TFC7MedConf28FavoritesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 195 
    7 MedConf        0x000000010f1ced87 _TToFC7MedConf28FavoritesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87 
    8 UIKit        0x0000000111b56226 -[UITableView animateDeletionOfRowWithCell:] + 132 
    9 UIKit        0x0000000111b353fd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72 
    10 UIKit        0x0000000111a54da2 -[UIApplication sendAction:to:from:forEvent:] + 75 
    11 UIKit        0x0000000111b6654a -[UIControl _sendActionsForEvents:withEvent:] + 467 
    12 UIKit        0x0000000111b65919 -[UIControl touchesEnded:withEvent:] + 522 
    13 UIKit        0x0000000111dffa10 _UIGestureRecognizerUpdate + 9487 
    14 UIKit        0x0000000111aa1686 -[UIWindow _sendGesturesForEvent:] + 1041 
    15 UIKit        0x0000000111aa22b2 -[UIWindow sendEvent:] + 666 
    16 UIKit        0x0000000111a68581 -[UIApplication sendEvent:] + 246 
    17 UIKit        0x0000000111a75d1c _UIApplicationHandleEventFromQueueEvent + 18265 
    18 UIKit        0x0000000111a505dc _UIApplicationHandleEventQueue + 2066 
    19 CoreFoundation      0x00000001113e2431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    20 CoreFoundation      0x00000001113d82fd __CFRunLoopDoSources0 + 269 
    21 CoreFoundation      0x00000001113d7934 __CFRunLoopRun + 868 
    22 CoreFoundation      0x00000001113d7366 CFRunLoopRunSpecific + 470 
    23 GraphicsServices     0x0000000113603a3e GSEventRunModal + 161 
    24 UIKit        0x0000000111a53900 UIApplicationMain + 1282 
    25 MedConf        0x000000010f1debf7 main + 135 
    26 libdyld.dylib      0x0000000113bff145 start + 1 
    27 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

Я не уверен, что причиной исключения является, как это брошено, даже если я закомментировать строку, в которой я удалить объект из источника данных: ObjectManager.removeFavoriteObject(favorites[indexPath.row]). Ниже приведен метод removeFavoriteObject.

static func removeFavoriteObject(objectToRemove: PFObject) { 

    if contains(favorites, objectToRemove) { 
     if let favObject = objectToRemove as? Attendee { 
      favObject.isFavorite = false 
     } else if let favObject = objectToRemove as? Event { 
      favObject.isFavorite = false 
     } else if let favObject = objectToRemove as? Session { 
      favObject.isFavorite = false 
     } 
    } 

    favorites.remove(objectToRemove) 
} 
+1

Возможного Дубликату http://stackoverflow.com/questions/26277180/invalid-update-invalid-number-of-rows-in-section -0 оконечное-с-неперехваченное-ех http://stackoverflow.com/questions/21870680/invalid-update-invalid-number-of-rows-in-section-0 для Swift http://stackoverflow.com/questions/25588779/ios8-swift-deleterowsatindexpaths-crashes –

ответ

0

Попробуйте

static func removeFavoriteObject(objectToRemove: PFObject) { 

if contains(favorites, objectToRemove) { 
    favorites.remove(objectToRemove) 
    if let favObject = objectToRemove as? Attendee { 
     favObject.isFavorite = false 
    } else if let favObject = objectToRemove as? Event { 
     favObject.isFavorite = false 
    } else if let favObject = objectToRemove as? Session { 
     favObject.isFavorite = false 
    } 
} 
} 
0

попробуйте передать индекс объекта к этому removeFavoriteObject статический метод:

ObjectManager.removeFavoriteObject(indexPath.row) 

и, как вы передаете индекс элемента из избранного массива, он должен быть там. Поэтому нет необходимости проверять использование «if».

static func removeFavoriteObject(index: Int) { 

    var objectToRemove = favorites[index] 
    if let favObject = objectToRemove as? Attendee { 
     favObject.isFavorite = false 
    } 
    else if let favObject = objectToRemove as? Event { 
     favObject.isFavorite = false 
    } 
    else if let favObject = objectToRemove as? Session { 
     favObject.isFavorite = false 
    } 

    favorites.removeAtIndex(index) 
} 
0
tableview.beginUpdates() 
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
tableview.endUpdates() 

Надеется, что это помогает