Last selected Row in UITableView - ios

I have UITableView, in which if any row is selected i am storing some values. If any other row is selected i am displaying an alertView with warning that his previous selection will be gone. This alerView has 2 buttons YES and NO. If YES is selected go on to selecting the new row which is fine. But if no is selected i want reselect the previous row. I can post the code if asked. But it seems more of logical question than code oriented.
Thank you.

first store the clicked tableview index to your global variable in UITableView Delegate like below,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==yourTableview) {
yourMemberVariable=[indexPath row];
}
}
then check yourMemberVariable in UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)//OK button pressed
{
//do something
}else{
if(yourMemberVariable>someValue){ //perform your own conditions whatever
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:yourMemberVariable inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
}

You want to know the last row selected ,you can do this by setting
tableView.remembersLastFocusedIndexPath = true
in viewDidLoad function.
and then calling
tableView.indexPathForSelectedRow
wherever required.

It's simple. Just save previous row into class member. Good luck!

Keep track of the last selected item. On the select delegate for the table, store what was selected in a variable accessible througout the class.
NSInteger selectedRow = 0;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = [indexPath row];
}
Now you know what element to select if NO is pressed in the UIAlertView delegate
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

Related

How to limit the multi selection in UITableViewController

Here are tableView data source and delegate implementation, and I set the maximum selection is 5.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([multiOptions count] > 5) {
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
//show alert
}
...
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[multiOptions removeObject:selectedOption];
...
}
but here comes a question, if the options exceed the limit, the first click on cell will work just fine. but second time click the cell will call
didDeselectRowAtIndexPath
again, that's not what i expected, and I tried
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in didSelectRowAtIndexPath, it didn't work, can someone give me a hint, how to correct it? thanks.
You have to check this in tableView:willSelectRowAtIndexPath:
Have a look at the UITableView class reference
especially at the last sentence:
Return Value An index-path object that confirms or alters the selected
row. Return an NSIndexPath object other than indexPath if you want
another cell to be selected. Return nil if you don't want the row
selected.
So something like this will work within tableView:willSelectRowAtIndexPath::
if ([multiOptions count] > 5) return nil;

Deleting selected cells in uicollectionview

I have placed a button within a uicollectionviewcell and when that button is pressed, it is programmatically setting the cell as selected.
- (void) deleteItem:(id)sender
{
self.selected = YES;
[self.cellOptionsDelegate deleteItem];
}
It then delegates to the uicollectionviewcontroller to deleted the item that is selected.
- (void) deleteItem
{
NSArray* selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems];
// Delete the items from the data source.
[self.taskArray removeObjectAtIndex:[[selectedItemsIndexPaths objectAtIndex:0] row]];
// Now delete the items from the collection view.
[self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
}
However, when i get the selected items using the uicollectionview method indexPathsForSelectedItems, it is not seeing that I have selected the item and the list is empty. I am using the press to select delegate method for another functionality so I was hoping to do something along the lines of what I explained above. Is there a way to make this work or a better way to inform the controller that the button pressed in the cell was tied to a cell at a specific index path?
Thank you.
Just send a cell pointer from your cell's deleteItem method
- (void) deleteItem:(id)sender
{
self.selected = YES;
[self.cellOptionsDelegate deleteItem:self];
}
and change uicollectionviewcontroller's method to
- (void) deleteItem:(UICollectionViewCell*)cellToDelete
{
NSIndexPath indexPathToDelete = [self indexPathForCell: cellToDelete];
// Delete the items from the data source.
[self.taskArray removeObjectAtIndex:[indexPathToDelete row]];
// Now delete the items from the collection view.
[self.collectionView deleteItemsAtIndexPaths:#[indexPathToDelete]];
}
Do not forget to update '(void) deleteItem:(UICollectionViewCell*)cellToDelete' declaration in your *.h file.
Solved by sending the cell through the delegate method and using this:
NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell];
animated:
[self.collectionView performBatchUpdates:^ {
[_array removeObject:file];
[self.collectionView deleteItemsAtIndexPaths:#[indexPath]];
} completion:nil];

IOS UITableViewCell De-select a view once selected and returned to page

How can I deselect a cell when returning to a view?
I have an orange down state which is applied to a cell when selected - the cell navigates to a modal view when clicked - when i click back button the cell is still selected.
I have tried applying this code -
[tableView deselectRowAtIndexPath:indexPath animated:YES];
to my cellForRowAtIndexPath method - but it doesn't do anything!
Update - Having done a bit of research - It appears Ive missed some valuable information out of this question! - my table view is a UITableView embedded in a View Controller - not a UITableViewController - so it sounds like it doesnt have the available methods which are required for the suggestions so far..
You could use UITableViewController's clearsSelectionOnViewWillAppear property.
You should not call deselectRowAtIndexPath in cellForRowAtIndexPath method.
you can do this in viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
NSIndexPath *selectedIndexPath = [tableViewObj indexPathForSelectedRow];
if (selectedIndexPath != nil) {
[tableViewObj deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
}
Or you can write in didSelectRowAtIndexPath as well
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath: indexPath animated:YES];
}
This is the right approach
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath: indexPath animated:NO]; // first line in this method
// rest of code
}

UITableView and indexPathsForSelectedRows

I have a tableView that I have set allowsMultipleSelection to YES in storyboard.
EDIT
I was wrong about one thing... [tableView indexPathsForSelectedRows] does return a NSArray with 1 object in it during didSelectRowAtIndexPath.
However it does not work in cellForRowAtIndexPath after I reload the table so it will check which accessory (check mark or not) to apply.
In the original question I was trying to manually select the rows... Apparently that is handled by the tableView itself.. but somewhere along the way it is automatically deselecting my row as I never call deselectRowAtIndexPath on it...
Original Question:
For some reason when I set the cell to selected it does not change.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//cell does not update selected property to YES after this next line
cell.selected = YES;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
I suppose I can keep track of the selected index paths myself... but I can swear I used a method that involved indexPathsForSelectedRows previously with success...
You cannot set the selected property directly
So instead of
cell.selected = YES;
Use
[tableView selectRowAtIndexPath:TheIndexPAth animated:YES scrollPosition:UITableViewScrollPositionBottom];
please post your all codes for this class in pastbean and put your link here we need more information,
and also try:
//in your interface
#interface YourClass (){
NSInteger _selectedIndex;
}
// cellForRowAtIndexPath: method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// didSelectRowAtIndexPath: method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
}
Try [cell setSelected:YES animated:YES]
I'm pretty sure cell.selected is read only
A bit unfair since the question changed but.. for me.. the answer is either don't use
indexPathsForSelectedRows
or don't call
reloadTable
on your tableView.
I opted for the later, I decorated the accessory in didSelectRow and didDeselectRow instead of doing it on reload and just never reloading the table.
If someone can come up with a solution that involves both of the above, I will select that answer instead.

Delete records of a TableView inside a UIView

I'm trying to implement a TableView inside a UIView in Storyboard.
This is what I've achieved:
Now I want that the Edit button, if pressed, prepare table rows for deletion.
I put the Edit button on the Bar wit the following:
//add edit button
[buttons addObject:self.editButtonItem];
Moreover I've set the tableview delegate and datasource to the main view and events are fired. But how can I tell to the edit button that the rows of the table must be deletable?
Thank you in advance,
yassa
There is this delegate tabelView method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//some other bookkeeping stuff
}
Added code for the Edit buttton when pressed. Rename "toggleEdit" to whatever to fit your needed.
- (IBAction)toggleEdit:(id)sender //this method should get called when Edit is pressed
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
//The if ... else part is optional. You might need to make some change to fit your's.
// if (self.tableView.editing)
// [self.navigationItem.rightBarButtonItem setTitle:#"Done"];
// else
// [self.navigationItem.rightBarButtonItem setTitle:#"Delete"];
}

Resources