I have custom UITableViewCell which has button that when clicked reveals a checked box.
Now I am creating this in xib and using a tableView to load it.
My problem is I need to set the selection state of the cell to true.
But when I do that reused cells lose selection property.
Is there any way to force the tableView to accept the cells selection?
Try this:
NSIndexPath *idxPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:idxPath animated:YES scrollPosition:UITableViewScrollPositionNone];
Related
My goal is to be able to auto scroll through my UITableView cells while automatically performing an action on the cell that is in the top visible position in the view. I've already figured out how to auto scroll through my UITableView using the various scrollview delegate methods, but im still having trouble figuring out how to call an action on the desired cell once it's in the correct position in the feed.
Does anyone have any suggestions?
You can use this :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:desiredrow inSection:desiredsection];
[yourTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
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.
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.
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];
}
I'm adding cell into uitableview on some action event. The cell is added at the top position in uitableview. I want the newly added cell to be selected by default.
Just call the method selectRowAtIndexPath:animated:scrollPosition: of the table view after the cell is added, assuming the cell is added in index 0 of section 0:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewRowAnimationTop]