I need to keep last but one indexPath cell of table view and compare it to the current one on
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method.
I don't know how can I do it. I found answers about last cell selected, but it doesn't help me. If anybody has some solution, please let me know about it.
To keep the reference of last indexPath in tableview add this code in your cellForRowAtIndexPath:
if(indexPath == _indexPathReference)
{
//add your logic
}
...
...
if(indexPath.row == array.count - 1)
_indexPathReference = indexPath;
return cell;
Hope this helps!
You try this out:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:<Pass_Last_But_One_Row> inSection:<Pass_Correct_Section>]
Here,
Pass_Last_But_One_Row = cellDataModel.count - 1;
Related
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;
I have a list of NSStrings stored in an NSMutableArray, each value in the NSMutableArray has been assigned to a UITableViewCell. Using the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
How do I figure out which cell was selected. I'm looking for some kind of value through NSIndexPath or any other method to change the icon of a UITableViewCell when it is selected. After I know which cell has been selected, I'd reassign the cell to the new image, and then reload the data using: [self.homeworkTable reloadData].
I'm looking for some kind of simple return value, like 0 if the first cell is selected, etc. I've tried utilizing the NSIndexPath with no luck, as an NSIndexPath is not an NSString. How would I go about doing this?
EDIT
I understand the answer may already be on Stack Overflow, I simply haven't found it, and redirecting me to the answer would be very much appreciated.
You can get it directly didSelectRowAtIndexPath (that's the whole point!) using indexPath.row
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int index = indexPath.row;
//....
It will match the index of your datasource array.
Sometimes you will need to get it from outside didSelectRowAtIndexPath deleguate method, you can do it the following way:
NSIndexPath *indexPath = [self.homeworkTable indexPathForSelectedRow];
int index = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
You should use indexPath.row, this gets you the row that you selected in this tableview.
You should also check out the documentation on NSIndexPath:
https://developer.apple.com/library/Mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html
I want to navigate from a tableviewcell to another view by the cell's path.
I am using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == 0) {
[self performSegueWithIdentifier:#"segueToKnutene" sender:self];
}
As you might understand, this did not work.
Keep in mind that i am fairly new to xcode and objective-c.
In my head what this does is performing a segue to another view if the top cell in my tableview is pressed/released. And this is also what i want to do.
An NSIndexPath consists of a row and a section.
So you probably want to use
if (indexPath.section == 0 && indexPath.row == 0)
I have a "UITableView", I am trying to handle selected row and use its data accordingly,
I have written necessary code to take the selected row, unfortunately it seems it does not work
If i select nothing,
NSIndexPath *path = [tableViewDoctorName indexPathForSelectedRow];
int rowSelected = path.row;
rowSelected's value is 0;
But, when i select the row it is again 0.
I have only one row in a table, i am not sure does it matter?
How can i accurately determine the selected row in a "UITableView"?
Set the UITableView delegate and implement this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int rowSelected = indexPath.row;
}
indexPath.row start with 0 for first row, And you have only one row then it return always 0;
But if you want to get data of selected Row then you delegate method of UITableView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here you can get directly indexPath.row so you can get required data;
}
Of course how many rows you have in your tableView matters : if you only have 1, then its indexpath is (0 ,0) and this is the only one that can be (selected) in this case.
The selected row generally is highlighted (in blue) so you might want to store it in another property of your tableView delegate, if you don't want this blue selection to stay on-screen.
Typically, in your UITableViewDelegate :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// deselect the row, to avoid the 'blue' selection to stay on-screen
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// save your own copy of 'last' selected indexPath
self.mySavedIndexPath = indexPath;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndexPath = indexPath;
}
In outside the function,
use this to get selected row,
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: _selectedIndexPath.row inSection:0];
int rowSelected = indexPath.row;
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.