Knowing when scrollToRowAtIndexPath doesn't scroll - uitableview

After adding a row to a table, I'm using scrollToRowAtIndexPath to position the row in the middle of the screen, then relying on scrollViewDidEndScrollingAnimation to highlight the same row.
Sometimes, the row can't be scrolled; e.g., when it's one of the first in the table. In these cases, scrollViewDidEndScrollingAnimation isn't called so the newly added row isn't highlighted.
Is there a way to know whether or not scrollToRowAtIndexPath will have any effect on the table?

While inserting you would have to specify indexPaths to be inserted right?
Store these indexPaths publically and reload that row with highligted color in CellForRowAtIndexPath or reload the rows individually.
Eg:
Add the indexpaths of inserted row to the public array.
[tableView insertRowsAtIndexPaths:indexpathArray withRowAnimation:UITableViewRowAnimationFade];
self.colorIndexs = indexpathArray;
Then add this in cellForRowAtIndexPath
if ([colorIndexs count] > 0 && [colorIndexs containsObject:indexPath])
customCell.contentView.backgroundColor = [UIColor redColor];

Related

After reloaddata in tableview stay in the same position

The user will click a button in one of the cell and the table will reload the data.
I would like to let the user stay in the same position after the reload.
My table contain many section and each section has two row.
Each of them may have different height.
I have tried some of the methods provided by others but its doesn't work.
CGPoint offset = tableView.contentOffset;
[tableView reloadData];
[tableView setContentOffset:offset];
Is it related to my table that each row in each section has different heigt?
I have also check that the offset is correct after the table is reloaded.
But the appearance is wrong.
I found that the view of the table is not change after the setting the offset

Stop [tableView loadData] from deselecting row using Xcode 5 with UIViewController

Here is my program. I want to create a simple list of items that display a number. When the rows are tapped the number will increment by one.
EDIT: Is it proper to change the UI of a row in the didSelectRowAtIndexPath function?
I created a UIViewController in Xcode 5 through a storyboard and it does everything right except I can't seem to stop the [tableView reloadData] from deselecting my row after being tapped. Specifically, I want the row to turn gray and then fade out normally.
I have tried selecting the row and then deselecting the row programatically after calling [tableView reloadData], but it doesn't work.
I know that if I was using UITableViewController that I could just call [self setClearsSelectionOnViewWillAppear:NO], but I'm not.
Is there a similar property I can set for UIViewController?
Here is the code:
[tableView beginUpdates];
[counts replaceObjectAtIndex: row withObject: [NSNumber numberWithInt:newCount]];
[tableView reloadData];
[tableView endUpdates];
I feel I may not be describing what is going on. I have a row that uses UITableViewCellStyle2, which displays a label to the left and right. On the right aligned text is a number that increments each time the row is tapped. Simply updating the data structure does not solve the problem. I need to update it visually. I don't need or want to replace the row, unless I have too. I just want to update the right-aligned text field AND keep the row from being deselected immediately without animation. I can do one or the other, but not both.
Is there a way to just update the right-aligned text field while still staying true to the MVC model?
Remove the [tableView reloadData]; from the code. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates .
Call reloadData method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view's delegate or data source calls this method when it wants the table view to completely reload its data.
[tableView beginUpdates];
[counts replaceObjectAtIndex: row withObject: [NSNumber numberWithInt:newCount]];
[tableView endUpdates];
See the developer.apple section - reloadData
If you want to keep the selection after reload, the easy way is
NSIndexPath *selectedRowIndexPath = [tableView indexPathForSelectedRow];
[tableView reloadData];
[tableView selectRowAtIndexPath:selectedRowIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

UITableView rows not reloading with UITableViewRowAnimationNone

I have a UITableView and in its -tableView:didSelectRowAtIndexPath: method I want to change the appearance of two cells (the one that has just been selected and the one that was selected before). I do that with this method:
// reload both rows to change appearance
if (![indexPath isEqual:activeIndexPath]) {
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:activeIndexPath, indexPath, nil]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
With the parameter UITableViewRowAnimationAutomatic set it works great: the corresponding rows are updated with a smooth transition. But I want those two rows to update immediately without an animation.
I thought that UITableViewRowAnimationNone would do the trick but if I use that constant for the row animation the cells are not being updated at all

Why does my [tableview reloadData] execute too late?

Something weird is going on with my tableview when changing data. I have something like this:
// fadeout existing data
... // change data (new values for the cells are blank)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade out
}
// add one new row to the table
... // change data (just add one new row but leave the cells empty)
[TableView reloadData] // reload all of the data (new row should be added)
... // change data (just add values to the existing cells)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade in
}
In theory, at least what I think, this should work. But I had some glitches so I added NSLogs in the for loops and in the: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath so I noticed that my [tableView reloadData] executes after the first row of fade in loop!##
I was thinking about making some kind of a delay between reloadData and fade in loop, but I don't want to force it to work, I want it to work properly.
Is there a better way to accomplish what I'm trying to do?
Can I dynamically add one row at the bottom of the table without calling the 'reloadData' method?
Thanks.
Yes, just use the tableView insertRowsAtIndexPaths method, which will then call your dataSource to load those new rows.
To load a single row at the end of your table, you'd first update your data source to include the new row, then generate the indexpath using:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[yourdata count]-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexpaths:indexPaths withRowAnimation:whateveryouwant];
Remember, that will immediately call your tableView:cellForRowAtIndexPath method, so update your dataSource to include the extra row before inserting the new row into your table, and be careful that the row index in the indexPath you specify matches the newly added item in your data source.

Table view cell indexing when using insertRowsAtIndexPaths

Ok, I'm stuck. This is an extension of a previous post of mine. Here is what I am trying to do.
I have an Edit button on a navigation bar that when pressed adds a cell at the beginning of my one section table view. The purpose of this cell if to allow the use to add new data to the table; thus it's editing style is Insert. The remaining cells in the table are configured with an editing style of Delete.
Here is my setediting method:
- (IBAction) setEditing:(BOOL)isEditing animated:(BOOL)isAnimated
{
[super setEditing:isEditing animated:isAnimated];
// We have to pass this to tableView to put it into editing mode.
[self.tableView setEditing:isEditing animated:isAnimated];
// When editing is begun, we are adding and "add..." cell at row 0.
// When editing is complete, we need to remove the "add..." cell at row 0.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray* path = [NSArray arrayWithObject:indexPath];
// fill paths of insertion rows here
[self.tableView beginUpdates];
if( isEditing )
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
else
[self.tableView deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
// We nee to reload the table so that the existing table items will be properly
// indexed with the addition/removal of the the "add..." cell
[self.tableView reloadData];
}
I am accounting for this extra cell in my code, except I now have two index paths = [0,0] - the new cell and the old original first cell in the table. If I add a call to reload the table view cell in setEditing, the cells are re-indexed, but now my table view is no longer animated.
I want my cake and eat it too. Is there another way to accomplish what I am trying to do and maintain animation?
--John
You can do what you want but you need to keep your data source consistent with the table. In other words, When the table is reloaded, tableView:cellForRowAtIndexPath and the other UITableViewDataSource and UITableViewDelegate methods responsible for building the table should return the same cells depending on editing state that you are adding/removing in setEditing:antimated:.
So, when you insert/delete a cell in setEditing:animated: you need to also make sure your data source reflects the same change. This can be tricky if you are adding a special cell to the beginning of a section but the rest of the data is from an array. One way to do this is while reloading the table, if editing, make row 0 the add cell and use row-1 for your array index for subsequent cells. If you do that you'd also need to add one to tableView:numberOfRowsInSection: to account for the extra cell.
Another way would be to have a section for the add cell and it would have 0 rows when not editing, 1 row otherwise and you return the appropriate cell. This will also require you to configure your table and cell(s) appropriate depending on how you want things to look.

Resources