How to show the selected index cell at middle of tableview - ios

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

Related

What is difference between deselectRowAtIndexPath and cell setSelected?

What is difference between
[tableView deselectRowAtIndexPath:indexPath animated:NO];
and
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexpath];
[cell setSelected:NO];
???
In a UITableView a cell does not match a specific piece of your data. It can (and should most of the time) be reused and it is nil when not visible.
deselectRowAtIndexPath will set the indexPath as 'not selected' in your tableview, so when you scroll back and forth to that cell, it will stay unselected, because you told your tableview that whatever the cell you display at that indexPath it should be unselected.
With UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexpath];
[cell setSelected:NO]; you set the cell as unselected. However that cell can be used for other pieces of data, and it can even be nil if this indexPath is not displayed.
First one is programmatically deselecting the cell. So it will un-highlight the cell if the user has selected it already.
As for the second bit of code, I believe that is just a pointer or reference to one of the cells in your table view. You can use this code to edit a cell OUTSIDE of any of the table view delegate methods. So if you wanted to edit/update a label on one of your cells but from a random method (not a table view delegate method), then you could use that code to reference the label text property.
I think you should also check out the Apple Developer Library website. It explains all the different table view method/properties/etc in lots of detail:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
The concepts of "selected" is different for a tableview and for a cell. The tableview can have one or more rows selected that changes the state of the tableview, however changing the tableview cell selected state only affects its appearance and nothing else.

iOS7: Tableview doesn't load accessory changes

So I got this method that I call on didSelectRowAtIndexPath:
- (void)selectTableViewRow:(int)row {
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
(It's only three cells so no for-loop)
It works perfectly fine. When a cell gets selected, there appears a checkmark and the others get removed. But when I call this method in viewDidLoad nothing changes graphicly.
Do I have to reload the table view after setAccessoryType?
This is because your tableView will reload its data after -viewDidLoad is completed. This means that your table view will call -tableView:cellForRowAtIndexPath: in your table view data source, and it will use whatever cell accessory is specified there.
You should specify your cell accessories in tableView:cellForRowAtIndexPath:
At viewDidLoad time, your cells won't get created until they're needed. You have to be careful here, as cellForRowAtIndexPath, if done properly can generate entirely new cells, and throw out ones you already created. You should move your code for setAccessoryType into your cellForRowAnIndexPath, and have it check to see if each cell should be selected based on a variable which is set to the currently selected row and compare it to the indexPath.row.
The issue is that iOS can toss and reuse cells. If you set cell 1's accessory view in your routine, it might get scrolled off the end, and reused elsewhere, say now as cell 7. So cell 7 has the checked accessory view, and cell 1 is off screen.
In other words, cellForRowAtIndexPath feeds cells to the tableView. It's up to you to set their contents correctly based on the indexPath you get called with, especially if you reuse the cell - you have to clear the accessory view or set it for every cell that's called.

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

Selecting row while scrolling

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

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