contentOffsetForScrollingToRowAtIndexPath in swift - ios

i getting some response which contains some information, i stored value in dictionary format in NSMutableArray for example
{name :david, email : xxx.com},{name: david, email : yay.com},{name : david, email : zzz.com} {name: Annie, email : annie.com},{name: George, email : ger.com}
i have two label Name and Email. when i press Label Action i displayed all the usernames in that i selecting name as "daniel"
Now i going for second label called Email Action in that i want only email list of "daniel" its contains three mail i have to displayed in tableview
here i tried this code but didn't worked for me
let data = NameList.filter{($0 as! [String:String])["Name"]!.containsString(NameLabel.text!)}.map { x in return x["email"]!}
// print(data)
detailsArray = data
my output log as follows :
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '-[UITableView
_contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (2) beyond bounds (1) for section (0).

The error tells you that your table does not have any row at index 2. It means that your table view consists of 2 rows and maximum range of indexPath.row can be 1 (because row index of a UITableView starts from 0). Whenever you will call a row at index path beyond range of the table, it will throw an error.

Related

Invalid update: invalid number of sections

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).'
but i insert 1 and deleted one based on data source what i missed
self.states?.append(sortedStates) //Update state property
if (self.states?.count)! > 3 {
self.states?.removeFirst()
}
self.newsFeedTableView.beginUpdates()
self.newsFeedTableView.insertSections([(self.states?.count)! - 1], with: .none)
if (self.states?.count)! > 3 {
let statesForoldestStateTime = self.states?.first
self.newestStateTime = statesForoldestStateTime?.first?.createdAt
let indexpostion = (self.states?.count)! - 3
self.newsFeedTableView.deleteSections([indexpostion], with: UITableViewRowAnimation.none)
}
self.newsFeedTableView.endUpdates()
The error says it all. When if (self.states?.count)! > 3 is false. The only section would be inserted and not deleted.
You should update your data source accordingly. The number of sections method must return someArray.count. When you insert some section, make sure to update that some array, and when you delete some section, delete the element from some array. That will resolve the issue.

Can't get Parse.com PFUser data?

I have a UITableView that is populated with games that a user is currently playing. I have a Pointer column in the Game table called Turn that points to a PFUser in the User table. Instead of setting the cell text to the objectID of the Turn value, I'd like to instead set it to the player's first name, which is stored in the User table.
The problem is that when I println the Turn pointer object, it only contains the objectID and I can't pull the first_name. Here's my code:
let object = gameResults[indexPath.row]
let user = object["turn"] as! PFUser
let userString = "\(object.first_name)"
cell.textLabel?.text = userString
return cell
The cell text just reads "nil". How do I get the user data from the User table that relates to the Turn pointer column?
The pointer column data are not included by default when using PFQuery.
Do this to tell your query to include the pointer data:
yourQuery.includeKey("turn")
This will get the user data from the user table and include it in the PFObject obtained.
UPDATE:
To get the first_name field (or any field for that matter), the syntax is:
let userSting = user["first_name"] as String

Swift - 1.2, Parse - 1.7.4, CachePolicy

I am trying to retrieve a query from the Parse database; however, when I run the app and click the button to go to the view controller that is going to retrieve the data from the database, my app crashes. When the app crashes, I get sent the AppDelegate.swift file.
This is the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Method not allowed when Pinning is enabled.'
I made sure to implemented Parse correctly in my project. Also, when I take out the if self.objects.count == 0 {} code block, the app runs fine when I go to the view controller that is retrieving the data from the database. But only this time, there are no objects in my tableview list when there are objects in my parse database. Thanks in advance.
override func queryForTable() -> PFQuery {
let query = PFUser.query()
if searchInProgress {
// We are looking for the string contents in the search bar to match the names in the parse username category.
query?.whereKey("username", containsString: searchString)
}
// From the objects aleady loaded...
if self.objects?.count == 0{
// If we have not already loaded the elements from our database, then it will use the elements that have already been downloaded when we have already run the app
query?.cachePolicy = PFCachePolicy.CacheThenNetwork
}
query?.orderByAscending("username")
return query!
}
You can't use pinning and cache policy at the same time, hence the inconsistency exception.
http://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/cachePolicy

Understanding UICollectionView insertSections:

I have a UICollectionView in my iOS application. I'd like to use the various insert... methods to add data, primarily for the animations provided by iOS when using these methods (instead of reloadData:).
If I try to add a section, however, I get an exception from iOS. My code is extremely straightforward:
_regions = [NSMutableArray array];
[self.collectionView reloadData];
NSLog (#"Before attempting to add section, collection view has %ld sections", [self numberOfSectionsInCollectionView:self.collectionView]);
[_regions addObject:#"First Region"];
[self.collectionView insertSections:[[NSIndexSet alloc] initWithIndex:0]];
NSLog (#"After attempting to add section, collection view has %ld sections", [self numberOfSectionsInCollectionView:self.collectionView]);
So I add an object to my _regions array, which I use to implement the data source, then I call insertSection. My first NSLog works:
2015-03-24 11:25:39.052 MyApp[63858:17968323] Before attempting to add section, collection view has 0 sections
but then iOS throws an exception:
2015-03-24 11:25:42.082 MyApp[63858:17968323] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).'
I implement numberOfSectionsInCollectionView in pretty straightforward manner:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return _regions.count;
}
If I reverse the calls (call insertSections before adding the string to the _regions array), I get a different exception:
2015-03-24 11:33:17.472 MyApp[63965:18001840] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert section 0 but there are only 0 sections after the update'
When should I modify (add an element to) _regions, relative to when I add the section to the UICollectionView?
Thanks so much for any help from experienced UICollectionView programmers . . .

NSRangeException - NSArrayM objectAtIndex

EDIT - SOLVED: the problem was when I created the string with all the objects to put into the tableview. I used the '?' to separe each object and one of those contained that character so the array broke.
[myString appendFormat:#"%#$%#$%#$%#?",
[currentCampeggioDict valueForKey:#"id"],
[currentCampeggioDict valueForKey:#"nome"], [...]
NS Array *elencoCampeggi=[myString componentsSeparatedByString:#"?"];
That's it.
I have this problem: my app receives from an URL a XML file with a list of objects. The tableview is populated by those objects. There are so many items, so I have to call several times the url, changing a parameter in it for example :
http://www.myurl.com?method=xxx&PAGE=1
http://www.myurl.com?method=xxx&PAGE=2
etc..
The last cell of the tableview contains the sentence "Click to load next 25 objects" and it changes the page number for the URL, delete itself and reload the url, adding the new objects to the tableview.
After 4 times everything works great, but when I press the cell for the fifth, an exception appears with the following text:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 17 beyond bounds [0 .. 16]'
Any help? Thanks.
EDIT: some code: this is the part where I delete the last cell
-(void) loadTestData {
// fill moviesArray with test data
NSInteger lastRow = [CampeggiArray count]-1;
if (lastLoadedPage > 1){
[CampeggiArray removeObjectAtIndex:lastRow];
[self.tableView reloadData];
}
[...]
Issue is because you are returning wrong row count in your tableview delegate method. You need to identify the total no of objects to be listed in the tableview and return the count in your tableview delegate method. This is not your page size.
For each hit you need to keep on appending the new objects to the already existing datasource(NSArray) and reload the table.There will be no objects in the datasource during initial hit.
I guess, you are returning 16 instead of 15 in the delegate method, so you wil get crash with exception. Don't hard code values in your delegate method, just get the total number of objects(entire set) and return the count of it.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [totalObjects count];
}

Resources