I am using the following code in my application.
NSIndexPath* ipath = [NSIndexPath indexPathForRow:[sectionInfo numberOfObjects]-1 inSection:section];
NSLog(#"ipath %#",ipath);
[self.myTableView scrollToRowAtIndexPath:ipath atScrollPosition: UITableViewScrollPositionTop animated:NO];
And getting the log like this
ipath {length = 2, path = 1 - 9}
But the application crashes with getting the error like
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
but if I run the project after commenting like
//[self.myTableView scrollToRowAtIndexPath:ipath atScrollPosition: UITableViewScrollPositionTop animated:NO];
it works fine.I am using fetchedresultcontroller for populating table view and object at indexpath is not null.Any help will be appreciable?
Use this code it may help you
UITableViewCell *cell = (UITableViewCell *)[yourTableName cellForRowAtIndexPath:indexPath];
[yourTableName setContentOffset:CGPointMake(cell.frame.origin.x, cell.frame.origin.y) animated:NO];
Related
I have an uitableview with a collection view nested inside. Both the Collectionview and the Tableview have just 1 section. In the collectionview cells there are uiimageviews. I'm downloading pictures from a server, and when the picture is downloaded, I'd like to display them in the image view. I'm calling reloadItemsAtIndexPaths to reload the content of the cell to display the picture. This is what i found out so far:
if i'm using
[NSIndexPath indexPathWithIndex:[secondArray indexOfObject:dictionaryToRefresh]]
as the index to refresh in reloadItemsAtIndexPaths, then the first picture loads just fine in the first cell of the collection view. The reloadItemsAtIndexPaths does a nice fade-in effect too. Then it crashes badly with the error:
Assertion failure in -[UICollectionView
_endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /SourceCache/UIKit/UIKit-3347.44/UICollectionView.m:3835 2015-05-03
18:47:02.385 FBomb[3117:798045] *** Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason:
'attempt to delete section 1, but there are only 1 sections before the
update'
if I use
[NSIndexPath indexPathForItem:[secondArray indexOfObject:dictionaryToRefresh] inSection:0];
All the pictures load just fine! But there is no fade-in effect, which is what i'm trying to achieve. Any ideas as to I can overcome this issue? I'd like to display the pictures with the fade in effect (I really can't understand why it doesn't happens)
Thanks in advance and pardon typos!
EDIT1
Full code:
-(void) reloadItemWithDictionary:(NSDictionary*)dictionaryToRefresh{
BOOL dictionaryFound = FALSE;
NSIndexPath *= [[NSIndexPath alloc]init];
NSIndexPath *indexPathSecondArray = [[NSIndexPath alloc] initWithIndex:0];
for (NSDictionary *firstDictionary in megaArray)
{
NSArray *secondArray = [firstDictionary objectForKey:#"subArray"];
if ([secondArray containsObject:dictionaryToRefresh]){
indexPathFirstArray = [NSIndexPath indexPathForRow:[megaArray firstDictionary] inSection:0];
//THIS FADES THE FIRST ITEM ONLY, THEN CREASHES
//indexPathSecondArray = [NSIndexPath indexPathWithIndex:[secondoArray indexOfObject:dictionaryToRefresh]];
//THESE WORKS, BUT THEY DON'T FADE
indexPathSecondArray = [NSIndexPath indexPathForItem:[secondoArray indexOfObject:dictionaryToRefresh] inSection:0];
//indexPathSecondArray = [NSIndexPath indexPathForRow:[secondoArray indexOfObject:dictionaryToRefresh] inSection:0];
dictionaryFound = TRUE;
break;
}
}
if ( dictionaryFound == TRUE){
dispatch_sync(dispatch_get_main_queue(), ^{
UITableViewCell *tableCell = [tableWithCollectionView cellForRowAtIndexPath:indexPathFirstArray];
UICollectionView *collectionViewInTableCell = (UICollectionView *)[tableCell viewWithTag:1000];
[collectionViewInTableCell performBatchUpdates:^{
[collectionView reloadItemsAtIndexPaths:#[indexPathSecondArray]];
} completion:^(BOOL finished){
}];
});
}
}
What happens when you try this instead:
NSUInteger x[] = {[secondArray indexOfObject:dictionaryToRefresh] , 0};
//I don't know if the first element is section or item. try both.
//NSUInteger x[] = {0, [secondArray indexOfObject:dictionaryToRefresh]};
NSIndexPath *path = [[NSIndexPath alloc] initWithIndexes: x length: 2];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:path]];
indexPathWithIndex creates an instance by just using one NSInteger whereas UITableView and UICollectionView requires two values for item/row and section
using this line of code in viewDidLoad after my table get reloaded.
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
Unfortunately, it's not working for me.
throwing Exception:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).
i want to hide header section of tableView on application start.
header section contain searchBar. so i want it to be hidden when app start.
i searched really hard but couldn't find the exact answer to my problem. so as last resort, i had to type it myself.
Regards,
A Naive iOS Developer.
This happens when a section has no rows. In this case use NSNotFound. You can find this in UITableView Class Reference.
Obj-C
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
Swift
let scrollIndexPath = NSIndexPath(forRow: NSNotFound, inSection: 0)
tableView.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: .Top, animated: true)
I am entirely confused. I am modifying my table and it's crashing while up via adding/deleting rows and reloading sections. So I stopped adding and deleting rows, and it still crashes. So I stopped reloading sections and it still crashes. So I just put the code:
[self.tableView beginUpdates];
[self.tableView endUpdates];
with this code it crashes, without it's fine.
Why?
2014-09-12 21:52:41.257 weatherApp[12616:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(0x2ea84f83 0x392ffccf 0x2e9bb7cb 0x3149eaef 0x3149cd8f 0x3149c843 0x31474e5b 0xeb82d 0xed059 0x313d48f7 0x3137bc27 0x3137b47d 0x312a1d59 0x30f1f62b 0x30f1ae3b 0x30f1accd 0x30f1a6df 0x30f1a4ef 0x30f6dc49 0x33d3a75d 0x2f770451 0x2ea44ea9 0x2ea4fa67 0x2ea4fa03 0x2ea4e1d7 0x2e9b8ebf 0x2e9b8ca3 0x33912663 0x3130514d 0x107599 0x3980cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Edit: Be advised I am calling this update in cellForRowAtIndexPath because I need to check for an update as new cells are displayed. The code that causes this update is the [self updateSection code, which right now only runs begin updates, end updates.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WFHNotamTableViewCell *cell = (WFHNotamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
if (!cell)
{
NSLog(#"Dequeue blank cell");
}
NSLog(#"Loading %d - %d", indexPath.section, indexPath.row);
NSString *group;
NSString *icao = [metarManager getIdentifierAtTotalIndex:indexPath.section andReturnGroup:&group];
NSMutableDictionary *notamDict = [notamManager getNotamFor:icao atVisibleIndex:indexPath.row];
NSDate *lastUpdate = [notamDict objectForKey:#"timeDownloaded"];
[self updateSection:indexPath.section andLastUpdate:lastUpdate];
return cell;
Thank you!
I have a NSFetchedResultController with different section.
I have a crash when I try to search using UISearchDisplayController :
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit/UIKit-2372/UITableViewRowData.m:1630
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x1d2c4120> 2 indexes [0, 1])'
I checked and my search array has indeed two entries (the expected result):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
It returns 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
It returns 2
Funny thing, if I have only one section, it works perfectly.
Help please! :)
Not sure if you figured it out, I would think you did but assuming you are using
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
in your cellForRowAtIndexPath
do
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
also make sure you are using self.tableView
i have a TableView which loads a detailview in didSelectRow. I have no problems with the data's.
I am using coreData and to populate the TableView i am using a NSFetchedResultsController.
Now, i want to make a next and previous function in the detailView to jump to the next or previous Table(row) Element. Next Previous like in the Mail-App.
I know that i have to use a button with IBAction.
But how can i make this function?
thanks,
brush51
Edit 1:
I have it done so:
- (IBAction) previousCard:(id) sender {
NSLog(#"previousBtn");
DashboardViewController *parent = (DashboardViewController *)self.parentViewController;
NSIndexPath *actualIndexPath = selectedRow2;
NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
NSLog(#"newIndexPath: %#", newIndexPath);
[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; //HERE I GET SIGABRT
}
But i get an error:
-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0'
How to solve this?
With your UITableView, you can get the selected row by:
- (NSIndexPath *)indexPathForSelectedRow
And set the selection by incrementing the indexPath.row:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
then you action could be:
-(IBAction)nextCell:(id)sender {
NSIndexPath * actualIndexPath = myTableView.indexPathForSelectedRow;
NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row+1 inSection:actualIndexPath.section];
[myTableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
for that you can manage with your DataSource array.
when you load your detail view at that time you pass your index of array(indexPath.row). by that index you fetch data in detail view. and using increment and decrement in that index you get data for next and previous cell.
Steps
1) setup your tableView.
2) when redirect to DetailView at that time set one int variable for store current indexPath.row . so define one int in detail View.
3) using next and previous button manage indexpath.row which is in int variable.
and represent appropriate data from Source array.
You can use:
(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
To get the cell you want (next or previous).
And then set the returned cell "Selected" property to YES.
cell.selected = YES