UITableView crashes because "Attempt to create two animations for cell" (iOS 7) - ios

In an UITableView I am trying to exchange the position of two sections and to add a new row into one of them by using a batch update.
BEFORE:
+-------------------------------------+
| |
| |
| SECTION A - HEADER - TITLE |
|-------------------------------------|
| SECTION A - ROW X |
|-------------------------------------|
| SECTION A - ROW Y > |
|-------------------------------------|
| |
| |
|-------------------------------------|
| SECTION B - ROW X |
|-------------------------------------|
| |
| |
+-------------------------------------+
AFTER:
+-------------------------------------+
| |
| |
|-------------------------------------|
| SECTION B - ROW X |
|-------------------------------------|
| |
| |
| SECTION A - HEADER - TITLE |
|-------------------------------------|
| SECTION A - ROW X |
|-------------------------------------|
| SECTION A - ROW Z - NEW ROW | <---------
|-------------------------------------|
| SECTION A - ROW Y > |
|-------------------------------------|
| |
| |
+-------------------------------------+
Here is my code:
[self.tableView beginUpdates];
[self.tableView moveSection:from toSection:to]; // from: 0, to: 1
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // indexPath.section: 1, indexPath.row: 1
[self.tableView endUpdates];
But the UITableView instance crashes and outputs the following:
2014-06-29 19:45:07.486 YouTube[3312:60b] *** Assertion failure in -[_UITableViewUpdateSupport _setupAnimationsForNewlyInsertedCells], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableViewSupport.m:1173
2014-06-29 19:45:07.488 YouTube[3312:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102447495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001021a699e objc_exception_throw + 43
2 CoreFoundation 0x000000010244731a +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000101d42f19 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
4 UIKit 0x000000010101166c -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 7491
5 UIKit 0x000000010101aa81 -[_UITableViewUpdateSupport _setupAnimations] + 193
6 UIKit 0x0000000100e10615 -[UITableView _updateWithItems:updateSupport:] + 1639
7 UIKit 0x0000000100e0c000 -[UITableView _endCellAnimationsWithContext:] + 11615
...
Is it not possible to move a section and add a new row at the same time in a batch update?

I know this is an old question, but just ran into this myself on iOS 11.3. We have code that diffs old and new models and generates UITableView updates, and it explodes with the "more than one animation for cell" exception when a section is both moved and has any changes to its cells (insert/delete/update).
The only solution is to delete/insert the section in this case, rather than move. You could either always do this (and probably animate sub-optimally), or do what we do and track when cells change and do the move only when safe.

You should change dataSources as well.
- (void)exchangeTableSection
{
[self.tableView beginUpdates];
NSMutableArray *temp = self.data1;
self.data1 = self.data2;
self.data2 = temp;
[self.data1 insertObject:#"ROW Z - NEW ROW " atIndex:1];
[self.tableView moveSection:0 toSection:1];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (0 == section) {
return self.data1.count;
} else if (1 == section) {
return self.data2.count;
}
return 0;
}

Related

Grails removeFrom() issue

Inside my Applicant domain class I have the following:
static hasMany = [recommendationFiles:ApplicantFile]
static mapping = {recommendationFiles joinTable: [name:"LETTER_FILES", key: "APPLICANT_ID", column: "LETTER_ID"]}
When I do the following:
def applicant = Applicant.findByENumber(session.user.eNumber)
def applicantFiles = applicant.recommendationFiles
println applicantFiles
applicantFiles.each {
applicant.removeFromRecommendationFiles(it)
}
applicant.save(flush:true)
I get this as an error which makes no sense to me:
| Error 2015-04-08 10:41:59,570 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver - ConcurrentModificationException occurred when processi
ng request: [POST] /scholarshipsystem/specialized/index - parameters:
_action_reUpload: Re-Upload
Stacktrace follows:
Message: null
Line | Method
->> 793 | nextEntry in java.util.HashMap$HashIterator
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 828 | next in java.util.HashMap$KeyIterator
| 106 | reUpload in scholarshipSystem.SpecializedController$$EP9HMKbd
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run . . . in ''
^ 662 | run in java.lang.Thread
There's a few ways to make this work. One way is to convert the list to an array and iterate over it.
def list = applicantFiles.toArray()
list.each {
applicant.removeFromRecommendationFiles(it)
}
Another way, if you're just removing the entire collection would be to...
applicant.recommendationFiles*.delete()
applicant.recommendationFiles.clear()

Insert sections at the beginning of UICollectionView

I want to add some sections to UICollectionView. insertSections at the index zero didn't work for me. So my idea was to insertSections at the end and then use moveSection:toSection: to move the elements from the end to the beginning. Here I get
NSInternalInconsistencyException Reason: attempt to move section 36, but there are only 36 sections before the update
I can only provide C# code, but you can also post Objective-C/Swift.
this.controller.CollectionView.PerformBatchUpdatesAsync (
delegate() {
nint sectionsBefore = this.controller.CurrentNumberOfSections;
this.controller.CurrentNumberOfSections += 12;
this.controller.CollectionView.InsertSections(NSIndexSet.FromNSRange(new NSRange(sectionsBefore,12)));
for(nint i=sectionsBefore; i<=this.controller.CurrentNumberOfSections; i++){
this.controller.CollectionView.MoveSection(i,0);
}
}
);
Edit:
Here is an output of the variables:
sectionsBefore: 36
CurrentNumberOfSections: 48
Range: <NSIndexSet: 0x7a77b9b0>[number of indexes: 12 (in 1 ranges), indexes: (36-47)]
36
37
38
39
40
41
42
43
44
45
46
47
If you have 36 sections then then your valid section indexes are 0-35, but your loop will end up calling MoveSection(36, 0). You should use < instead of <=.

tableView crashes on end up with more than 16 items

This is very confusing.
I have a UITableView, which updates and works fine until it gets more than 16 items then it crashes when trying to endUpdates after calling insertRowsAtIndexPaths.
The NSIndexPaths being added are all valid. -numberOfRowsInSection returns the correct number. It is not throwing an error related to the data set, rather it crashes with
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
when endUpdates is called on the tableView.
The data source is all there, the NSIndexPaths are fine. the code works fine between 0 and 16 rows, but when I add a 17th it crashes. Additionally if I start it with 22 items it works fine, when I add the 23rd it crashes... if I call reload data instead of doing the update and insert process it works fine, so it's nothing to do with the data itself, and it shouldn't be anything to do with how I'm inserting the rows since it works through 16...
I'm completely perplexed. Here is my update method. It is being called on the main thread at all times.
- (void)updateConversation:(NSNotification*)notification
{
NSDictionary *updateInfo = [notification userInfo];
//NSLog(#"Got update %#", updateInfo);
if ([[updateInfo objectForKey:#"success"] integerValue] == YES) {
[self updateConversationUI];
int addedStatementCount = [[updateInfo objectForKey:#"addedStatementCount"] intValue];
if (addedStatementCount > 0) {
//[self.tableView reloadData];
[self.tableView beginUpdates];
int previousStatmentCount = [[updateInfo objectForKey:#"previousStatmentCount"] intValue];
NSLog(#"owner %i, Was %i, now %i, change of %i", self.owner, previousStatmentCount, (int)self.conversation.statements.count, addedStatementCount);
NSMutableArray *rowPaths = [[NSMutableArray alloc] init];
for (int i = previousStatmentCount; i < previousStatmentCount + addedStatementCount; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
[rowPaths addObject:path];
}
[self.tableView insertRowsAtIndexPaths:rowPaths withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:[rowPaths lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
}
The rest of the crash past [self.tableView endUpdates] is UITableView
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x000000010189b795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001015fe991 objc_exception_throw + 43
2 CoreFoundation 0x0000000101852564 -[__NSArrayM insertObject:atIndex:] + 820
3 UIKit 0x0000000100317900 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke691 + 173
4 UIKit 0x00000001002b5daf +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460
5 UIKit 0x00000001002b6004 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
6 UIKit 0x00000001003174cb -[UITableView _updateWithItems:updateSupport:] + 2632
7 UIKit 0x0000000100312b18 -[UITableView _endCellAnimationsWithContext:] + 11615
8 Dev App 0x0000000100006036 -[ConversationViewController updateConversation:] + 998
9 CoreFoundation 0x00000001018f121c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
10 CoreFoundation 0x000000010185370d _CFXNotificationPost + 2381
11 Dev App 0x00000001000055ac -[ConversationManager postNotification:] + 92
12 Foundation 0x0000000101204557 __NSThreadPerformPerform + 227
13 CoreFoundation 0x000000010182aec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010182a792 __CFRunLoopDoSources0 + 242
15 CoreFoundation 0x000000010184661f __CFRunLoopRun + 767
16 CoreFoundation 0x0000000101845f33 CFRunLoopRunSpecific + 467
17 GraphicsServices 0x00000001039a23a0 GSEventRunModal + 161
18 UIKit 0x0000000100261043 UIApplicationMain + 1010
19 Dev App 0x0000000100003613 main + 115
20 libdyld.dylib 0x0000000101f2a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
This seems like a bug in the OS, the stack would indicate that it's something to do with the animation block but it happens no matter what I set the animation to, including none.
And to re-state. This works through 16 items, then inserting items past that causes a crash. It is also called at initial setup to load data, and it will load any number of items there, including well over 16. But when called again, purely as an update from 16 or more to something higher it will crash.
Most bizarre issue I've yet to encounter with table views...
under iOS 7 on device and simulator, latest Xcode for reference.
It seems that the problem is caused by my call of scrollToRowAtIndexPath (even though it crashes before it gets there...) combined with implementing tableView:estimatedHeightForRowAtIndexPath: by removing the row height estimate the crash went away... still seems like a bug in the animation system for tables to me. Thankfully I don't need the estimated row height, I had forgotten I had implemented it (trying to play nice by iOS 7 bites me again).
Place strategic breakpoints when you are incrementing the statement count, go through the loop of adding the rows as many times as you need to, and locate the statement that it's causing the crash, at that moment take a look at your objects and look for nil values as the error you are having it's trying to insert a nil object(or un-existent) from an Array.
I would personally recommend you to go through this loop entirely while keeping an exe for the path and previous statement count .
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
in my case the tableView:heightForHeaderInSection: method has been missing,
hadn't implemented the tableView:estimatedHeightForRowAtIndexPath method so this also could be solving the problem if you aren't using tableView:estimatedHeightForRowAtIndexPath:
Your problem is that you don't update the dataSource the tableView is using.
if you insert a row, you need to insert the appropriate object to the array the tableView is using.
Goodluck!

My custom class has all the same variables

Below is a simplified example of my problem. I am trying to store the same object with different values. But when I set a new value, all the values change.
StopsOnRoute.h
#interface StopsOnRoutes : NSObject
#property (nonatomic) NSUInteger start_route_id;
#property (nonatomic) NSUInteger start_stop_id;
#property (nonatomic) NSUInteger start_time;
#end
FirstViewController.m
- (void)viewDidLoad
{
NSMutableArray *route1 = [[NSMutableArray alloc] init];
StopsOnRoutes *stopOnRoutes = [[StopsOnRoutes alloc] init];
int p_time = 0;
int p_route = 0;
int p_stop = 0;
while(p_time<10){
p_time = p_time + 1;
p_route = p_route + 1;
p_stop = p_stop + 1;
[stopOnRoutes setStart_time:p_time];
[stopOnRoutes setStart_route_id:p_route];
[stopOnRoutes setStart_stop_id:p_stop];
[route1 addObject:stopOnRoutes];
}
}
Unexpected output of array route1:
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
Expected output of array route1:
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5
6 | 6 | 6
7 | 7 | 7
8 | 8 | 8
9 | 9 | 9
10 | 10 | 10
You are reusing the same object instance over and over. Create a new one each time in the loop.
while(p_time<10){
StopsOnRoutes *stopOnRoutes = [[StopsOnRoutes alloc] init];
p_time = p_time + 1;
p_route = p_route + 1;
p_stop = p_stop + 1;
[stopOnRoutes setStart_time:p_time];
[stopOnRoutes setStart_route_id:p_route];
[stopOnRoutes setStart_stop_id:p_stop];
[route1 addObject:stopOnRoutes];
}

iOS: Resizing and positioning a new view added to app started in landscape

I think this has probably been asked, but after reading a lot, I'm not sure I have found an answer.
When the app is rotated to landscape, I'm adding a new view to the main view. The new view is constructed and added in code like this:
UIView * newView = ....
[rootView addSubview:newView];
But because the simulator is rotated to landscape when I add the new view I get this:
+------------------------------------------------------+
+ +---------------------------------------------+ +
+ | | | +
+ | | | +
+ | newView | rootView | +
+ | (Landscape | (Landscape) | +
+ | but portrait size) | | () +
+ | | | +
+ | | | +
+ | | | +
+ | | | +
+ +---------------------------------------------+ +
+------------------------------------------------------+
So I added newView.bounds = rootView.bounds; newView.center = rootView.center; thinking that would position the newView directly over the top of the rootView, but instead I got this:
+------------------------------------------------------+
+ +---------------------------------------------+ +
+ | | +
+ | | +
+ |--------------------------+ rootView | +
+ | | (Landscape) | +
+ | newView | | () +
+ | (Landscape size, | | +
+ | but offset) | | +
+ | | | +
+ | | | +
+ +---------------------------------------------+ +
+------------------------------------------------------+
I'm trying to figure out why they are different even though I've set the bounds and centers to be the same.
Dumping out the views I get this:
rootView; frame = (0 0; 768 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x98870a0>>
newView ; frame = (-128 128; 1024 768); autoresize = W+H; layer = <CALayer: 0x6e470a0>>
So it appears that the rootView is still 768w x 1024h, just rotated by a transform, and after setting bounds and centre, the new view is 1024w x 768h and offset by 128 points.
After some more playing around, I came up with this:
// Position the new view offscreen.
CGFloat width = rootView.bounds.size.width;
CGFloat height = rootView.bounds.size.height;
newView.frame = CGRectMake(0, height, width, height);
[rootView addSubview:newView];
// Animate to the center of the view.
[UIView animateWithDuration:1.0 animations:^{
newView.frame = CGRectMake(0,0, width, height);
}];
It works, I'm just wondering if there is a better solution.

Resources