Delete a UICollectionViewCell from the cell - ios

I have a method that calls the below code within a UICollectionViewCell. The cell has a reference both to the UICollectionView and to the array that it is based off of. The below code fails with the error:
2014-11-20 13:41:08.976 MyApp[954:304515] *** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.16.14/UICollectionView.m:3917
2014-11-20 13:41:08.978 MyApp[954:304515] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x22f09d67 0x30768c77 0x22f09c3d 0x23bdcaa9 0x2656863d 0xe8e50 0xe8fa8 0x2680bcc1 0x23c10dc9 0x22ed0517 0x22ed0099 0x22ece303 0x22e1b981 0x22e1b793 0x2a1f4051 0x2640d981 0x1138f4 0x113930 0x30d04aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
The Code
//Resolves cell deletion
func deleteMe()
var path = collectionView.indexPathForCell(self)!
collectionArray.removeAtIndex(path.row)
collectionView.deleteItemsAtIndexPaths([path])
}
How do I delete this cell?

In this case, when assigning collectionArray or collectionView (I didn't test to see which, but my bet is on collectionArray) it was not an inout reference, but rather a copy. Array's have some odd functionality like this for performance issues. If I passed in a reference to the whole parent view (serviceView) and accessed using
serviceView.collectionArray.removeAtIndex(path.row)
serviceView.collectionView.deleteItemsAtIndexPaths([path])
it works fine. The datasource and the displayed cells were mismatched because the array was not being edited, a copy was.

Related

How to debug inside performBatchUpdates

I append objects in the collectionView model inside performBatchUpdates, something like that :
collectionView.performBatchUpdates({
for item in items{
// Append the different objects... example:
if item is Picture{
self.modelCollection.append(item, cellReuseIdentifier: "Picture")
}
}
}, completion: { completed in
self.isRefreshing = false
I am sorry but I can't post the original code. This code is inside a function called after the data is reloaded or when the device orientation changes to rearrange the collectionView cells. When the device is reloading the data and the orientation change at the same time I get an error
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'request for index path
for global index 536870881 when there are only 31 items in the
collection view'
I know it's not easy to understand the problem without the complete code but my problem consist on how to debug this error? I am almost sure that the problem is inside performBatchUpdates.I put a beakpoint in every single line but I get the error only in the completion closure without any other informations. Do you have an idea on what is going on or how to debug this ?

iOS error Terminating app due to uncaught exception 'NSRangeException Range or index out of bounds'

I recently picked up doing iOS development.
I am new to the whole thing, so please forgive my ignorance.
I ran into an issue in my app where when i start a stream (like streaming live audio), the app crashes (ipod touch 5th gen running iOS 7.1).
I get this error:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'.
Can someone help me in diagnosing this issue and possible solutions?
Thanks!!
ironmantis7x
EDIT: Here is the full error message:
2014-06-18 13:18:39.181 miraathradios[845:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'
*** First throw call stack:
(0x2f6f0fd3 0x39e3bccf 0x2f6f0f15 0x2f6364f7 0x63c65 0x63b35 0x60a8b 0x300d9c73 0x2f6bc25b 0x2f6bb72b 0x2f6b9f1f 0x2f624f0f 0x2f624cf3 0x34546663 0x31f7016d 0x588a9 0x587f8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
partial code:
- (JHTickerDirection)detecteDirection:(NSString *) title{
//NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSCharacterSet *letters = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
unichar ch = [title characterAtIndex:6];
if ([letters characterIsMember:ch]) {
return JHTickerDirectionLTR;
}else{
return JHTickerDirectionRTL;
}
}
When debugging, I strongly advise you to add 2 special breakpoints :
One for all exceptions.
And one for all "BAD_ACCESS" errors.
When those 2 breakpoints are enabled, xcode will automatically break on the faulting line instead of directly crashing without giving more informations.
To activate them, follow these steps :
open the breakpoint left tab, and click the "+" button in the bottom left corner.
select Add Exception Breakpoint.
Right click on the new breakpoint, and configure it this way :
Exception : All
Break : On throw
Action : Add Action
Options : leave the box unchecked.
Then add a symbolic break point ("+" button -> Add Symbolic Breakpoint)
Configure the breakpoint this way :
Symbol : malloc_error_break
don't modify all the other options
You're done for starting debugging !
So you're hard-coding the index to look for characterAtIndex: 6.
If the argument "title" passed in is anything shorter (say only 4 characters), or perhaps it's a unicode character split in 2 (like ä split into a and ¨), "ch" could be undefined and "out of range." Perhaps consider looping through "title" by character & seeing if it's within a given range of unicode characters, rather than hard-coding the index & checking against the entire alphabet string.

Assertion failure in -[NSIndexPath row] with AppList dataSource

See update below.
I'm writing a tweak and am using AppList. I am hooking into spotlight and creating a table off all installed applications. I am trying to use the dataSource to get the information (display identifier etc). The problem is if dataSource is defined... I get the following error:
SpringBoard[1622] <Warning>: *** Assertion failure in -[NSIndexPath row], /SourceCache/UIKit/UIKit-2380.17/UITableViewSupport.m:2680
SpringBoard[1622] <Warning>: ***** AltKeyboard Uncaught Exception: Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible. *****
or
SpringBoard[1890] <Warning>: ***** AltKeyboard Uncaught Exception: *** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1] *****
SpringBoard[1890] <Error>: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
If dataSource is not defined and I use fake data (test name), the table is generated fine. When I print the dataSource there are only two items in it.
Ryan Petrich, on IRC, seemed to suggest that I was doing everything completely wrong but wasn't much help after that. Perhaps I am going about solving this problem incorrectly?
Code: http://pastebin.ca/2457626
Repository: https://github.com/twodayslate/ListLauncher
edit/update!
dataSource is giving me problems. It isn't fetching the same number of apps and thus I am getting index errors and the like. I have over 200 apps but dataSource is only fetching 2.
The following code gives no errors but only displays 2 items: http://pastebin.ca/2458961
edit!
I changed it so I just use ALApplicationList. I am able to get all the applications listed. However, when I attempt to scroll through the table, it crashes. Sorting and creating the list is really slow so I believe that might be causing the problem. I'm no pro at obj-c so I'm sure there is a better way than what I am doing. http://pastebin.ca/2459318
Your problem is twofold. First, UITableView expects to be handed index paths containing both a row and a section, where you've provided it with only a row. Second, you cannot expect unchecked array access to succeed 100% of the time, so bounds check whatever access calls you might be using. It also wouldn't hurt to use some retains in the iVar assignments you've got in your code unless you don't need to hold a reference to it (but it looks like you do).
I figured out an alternative solution to ALDataSource. I sorted ALApplicationList.applications. The problem was that the instance of the sorted NSArray would keep disappearing. So I did [values retain] and everything worked! Reinitializing the sorted values was too slow and would crash the device.
Here is the working code that successfully lists all the apps: http://pastebin.ca/2459778

Zero object in array crashes the program, also after certain count

I have a program that grabs data from Instagram, puts it into dictionary and then I parse it.
Problems begin when I try to use that data, magically item number 0 crashes my program.
Also, after certain number next item also crashes the program.
Here is the code for UITableViewCell, that grabs text from the array/dictionaries and puts it into cell. As it looks, it crashes, if I add another check to start with number 1, it works, but crashes later when it reaches some count.
I don't understand why this happens, since I have the method that tells the TableView how many rows are in table, and it returns [self.loader.dataArray count], so it can't possibly try to load the thing that is out of bounds.
if (self.loader.dataArray[indexPath.row] != nil )
{
cell.textLabel.text = [[[self.loader.dataArray[indexPath.row] objectForKey:#"caption"] objectForKey:#"from"] objectForKey:#"full_name"];
}
Here is the error I am getting:
instagramClient[8254:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x3bb8f090'
The exception that you're getting does not indicate an out of bounds issue, it indicates that you have got an NSNull in your dataArray (or possibly in the "caption" object of one of the dictionaries in your dataArray or in the "from" object of one of those dictionaries, and so on). Try logging all of self.loader.dataArray when you get updates to that array and see if it contains any NSNull objects.

UITableView Exception After Rendering

I'm getting this error when a load a UITableView. It's occurring after the nib loads, but before either the cellForRowAtIndexPath, numberOfRowsInSection or other related methods are called.
The object it is backed onto is empty, but it still occurs when there is an object there (it's an NSMutableArray).
*** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit/UIKit-1860.2/UITableViewSupport.m:263
2011-08-12 12:24:00.616 WheresNext[684:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to resolve row for index path: <NSIndexPath 0x1d18f0> 2 indexes [0, 1]'
This doesn't however occur in the Simulator, only on devices.
Any ideas?
This error occurs often when it exists an inconsistency between the rows you add to your TableView and the number of rows return by tableView:numberOfRowsInSection:.

Resources