Selecting row while scrolling - ios

I have a program that selects rows at different times which works perfectly fine. But when i try to scroll down and try to select another row "out of view" while a row has already been selected by my program some where, it doesn't allow me to select that row which i clicked on. It only works if the my selected row is in the current displayed cells or display view. I have enabled multiple selection but still doesn't work.
-I've also find out that didSelectRowAtIndex isn't get called when selecting row outside the view where the program has already selected a row

I have found a solution. didSelectRowAtIndex didn't get called because my scroll position of tableView was set to UITableViewScrollPositionTop and this wasn't allowing me to select another row outside the view. All i did was to change it to UITableViewScrollPositionNone And adding scrollToRowAtIndexPathwith scrolling position to none will allow minimum scroll with selected row visible at bottom.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:*rows inSection:0]; // set to whatever you want to be selected first
[tableview selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
}

Related

How to show the selected index cell at middle of tableview

I am doing one application, that i got ten cells for each row.
And user can select the index of cell from list.
By default 4 cells are appear based on their height.
If user selected index as 4 then we can show the cell at middle using tableview provided method.
But if user select the 6th cell then how we can show at middle of tableview.
How to do this?
You can selected a cell at certern IndexPath .Using :selectRowAtIndexPath with a param UITableViewScrollPositionMiddle.
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];

UITableView scroll to element programmatically

I have array of 20 elements, and table view that can show only 5 of them. On start of view, I am selecting 1 item, and I want to show that item in table view by selecting it. When I choose one of first 5, selection is visible. When I select 5+ element, I want that element to be visible too by moving content offset. How can I scroll TableView to specific element from list inside tableview array? In which function of table view delegate do I do that?
I tried setting content offset, and scrollToRowAtIndexPath inside cellForRowAtIndexPath method, but it is not working correctly. Any ideas?
Calling scrollToRowAtIndexPath: at cellForRow will not work..First you need to reload data of tableview and then you need to call as below after selecting an item..
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Hope it helps you..
Put your scrollToRowAtIndexPath inside ViewDidLoad or ViewWillAppear of the TableViewController.

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];

Easier way to select all rows in UITableView

I wish to select all rows in UITableView for which I am using a for loop as per below code:
- (void)selectAllRows
{
for (int row = 0; row < [self.tableView numberOfRowsInSection:1]; row ++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
I couldn't find any direct delegates on NSIndexPath class or on UITableView controller to select all the rows on UITableView cell. This is of 'n' complexity, but could there be a better way to select all the cells?
This is the only way to select all the cells. The real question is why are you wanting to select all the cells? Are you selecting them to get UITableView delegate callbacks? Are you selecting them for UI purposes?
You can't reference all cells in your tableView as the tableView doesn't "have" all of them as it uses reuse/dequeueing.
If by "UI purposes" you mean solely for the visual effect as opposed to, you could loop
through all visible cells in your tableview and set the alpha to 0.5 or something similar, and if you want to fade them out and in as if they were selected/deselected you could do some [UIView animate....] calls on all of them. If you were to do something like this, you might want to consider disabling scrolling for the short period of time your animation is occurring, because if you are doing animation on visible cells as soon as you scroll any of the new cells wouldn't have that animation. Once you're done animating, in the completion block, you can then re-enable scrolling.

Control does not immediately return to first row in UITableView after dismissing UIImageView in iOS

I have an iOS application where I am displaying a UITableView, which in turn is composed of custom rows that are made up of UITextFields, and UILabels. On first launch, I have a UIImageView that covers the UITableView until the user presses a button in order to dismiss the UIImageView (thus revealing the UITableView behind it).
However, my problem is that instead of returning control to the first row of the UITableView, it is selecting the last row of the UITableView, and on top of that, despite me making an explicit call to the method, "becomeFirstResponder", the UITextField in the last row of the UITableView does not allow me to enter text until I explicitly select a row from the table. Then and only then am I able to enter text. I am assuming that my method "cellForRowAtIndexPath" populates my table from the top down, which is why the last row of the table is being selected, and not the top (this is my guess, and if I am wrong please correct me).
What I would like to do is enable the very first row of the UITableView once the user dismisses the UIImageView AND have it become the "firstResponder" automatically, allowing the user to enter text right away without having to explicitly select a particular row.
My code that dismisses the UIImageView after pressing a button is here:
- (IBAction)beginDataEntry:(id)sender {
[_imageView removeFromSuperview];
[_cell.dataField becomeFirstResponder];
}
It is as if my UITableView is in a "limbo" state until I explicitly select a row from the table. What am I doing wrong?
Use UITableView scrollToRowAtIndexPath:atScrollPosition:animated: to display the correct row when the view is redisplayed.
It is always safer to use index paths to refer to table rows rather than keeping references to cells. So, here's an alternative:
- (IBAction)beginTireCountEntry:(id)sender {
[_imageView removeFromSuperview];
NSIndexPath firstCellPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath: firstCellPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
MyCell cell = (MyCell *)[self.tableView cellForRowAtIndexPath:firstCellPath];
[cell.dataField becomeFirstResponder];
}

Resources