how can i find out in which section i clicked? i need the number to send the item to another view:
- (IBAction)mapButton:(id)sender {
UIButton * myButton = sender;
int row=myButton.tag;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];
MapViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"mapView"];
self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
detail.currentStandort = self.selectedStandort;
[self.navigationController pushViewController:detail animated:YES];
}
Is the mapButton a subview of the UITableViewCell?
Then I would do the following:
NSView *contentView = [sender superview];
NSTableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [myTableView indexPathForCell:cell];
NSInteger section = cellIndexPath.section;
You can also use the row from the cellIndexPath, so you don't have to keep your tag value up to date (e.g. on reuse, reorder and delete) which makes your implementation less error-prone.
Related
I want to pass textview value on btn click on a particular cell index.
my code like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-----
[cell1.submitCommentBtn addTarget:self action:#selector(leftCommentBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; // here i want to pass textview object or its data
---
}
Try thiss
Using View Hierarchy
- (IBAction) leftCommentBtnClicked:(UIButton *)sender {
UITableViewCell *cell =(UITableViewCell *)sender.superview.superview.superview; //change with your class
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // using this indexpath you can get data from your datasource array
cell.textView; // this is your textview
}
OR
Using ConvertPoint
- (IBAction) leftCommentBtnClicked:(UIButton *)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
YourCell *cell = (YourCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textView; // this is your textview
}
- (void)leftCommentBtnClicked:(UIButton*)sender
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:(Your Table Name)];
NSIndexPath *indexPath = [(Your Table Name) indexPathForRowAtPoint:buttonPosition];
}
Here you get IndexPath, so you can get all the value of that particular row.
I want to pass NSIndexPath to #selector.
So I used following code, but there is no argument like withObject.
How can I do it?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// User taps expanded row
if (selectedIndex == indexPath.row){
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps different row
if (selectedIndex != -1){
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:1];
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps new row with unexpanded
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[favoriteButton addTarget:self action:#selector(favoritePressed:) withObject:indexPath forControlEvents:UIControlEventTouchDown];
[normalButton addTarget:self action:#selector(normalPressed) forControlEvents:UIControlEventTouchDown];
[halfButton addTarget:self action:#selector(halfPressed) forControlEvents:UIControlEventTouchDown];
}
-(void)favoritePressed:(NSIndexPath *)path{
}
There is a better way to do this. You can find the touch point on button click and convert that touch point in to your table frame, and through that point you can get indexpath.
-(void)favoritePressed:(UIButton *)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
}
Try like this:
-(void)favoritePressed:(UIButton *)sender{
//CustomCell : your custom UITableViewCell class name
CustomCell *cell = (CustomCell) [[sender superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
NOTE:
If you set button in content view of CustomCell this will work for you otherwise you need to add other superview for getting the cell.
The code below may work for certain condition but your button may be a inside other view inside a cell. In that case you may not find the cell.
CustomCell *cell = (CustomCell) [[sender superview] superview];
It is better to use it like this.
- (void)btnFavoriteTapped:(UIButton *)sender {
id view = [sender superview];
while ([view isKindOfClass:[CustomCell class]] == NO) {
view = [view superview];
}
CustomCell *selectedCell = (CustomCell *)view;
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
}
I have added a UIButton in my UITableViewCell and in the action of that button, I have added the following code to get the indexPath of the cell. But it keeps returning the error- 'NSInvalidArgumentException', reason: '-[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance 0x7cea3fc0'
Can anyone tell me what to do, here?
- (IBAction)navigateMap:(id)sender {
__strong UIButton *button=(UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//rest of the logic
}
FULL CODE
- (IBAction)navigateMap:(id)sender {
__strong UIButton *button=(UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
CombinedStations *inButton=[[CombinedStations alloc] init];
inButton=[_finalStationList objectAtIndex:indexPath.section];
_universalRegion->center.latitude=inButton.latitude;
_universalRegion->center.longitude=inButton.longitude;
NSString *launchUrl=#"";
launchUrl= [launchUrl stringByAppendingString:#"http://maps.google.com/maps?daddr="];
NSString *tmpLat = [[NSString alloc] initWithFormat:#"%f", _universalRegion->center.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:#"%f", _universalRegion->center.longitude];
NSString *llat=[tmpLat stringByAppendingString:[#"," stringByAppendingString:tmpLong]];
launchUrl=[#"http://maps.google.com/maps?daddr=" stringByAppendingString:llat];
launchUrl=[launchUrl stringByAppendingString:[#"&saddr=" stringByAppendingString:#"Current Location"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[launchUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
Try this, This one will be very easy way to get the indexpath pf the table.
- (IBAction)navigateMap:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableViewName];
NSIndexPath *indexPath = [tableViewName indexPathForRowAtPoint:buttonPosition];
}
button's superview is cell's contentView. Try getting superview of a button's superview:
UITableViewCell *cell = button.superview.superview;
IMHO this is a really bad design.
Try to get indexpath from the tableView. Simply create a delegate for that cell and fire it whenever the button is tapped.
Conform to the delegate within the owner of the tableView. Whenever you catch the callback ask to the tableview the indexPath of the incoming cell from the delegate.
- (void)cell:(SampleCell *)cell actionButtonTapped:(UIButton *)button
{
self.currentIndexPath = [self.tableView indexPathForCell:cell];
}
I have UICollectionView that has two UICollectionResuabeView(Custom View) and one UICollectionViewCell. The form a single entity in UICollectionView. I put Delete button on one of UICollectionResuabeView and by pressing it want to delete the row (if i call it right).
This is the screenshot form Storyboard:
I tried getting section of it my many means, but non works. Guess I just dont have enough knowledge of how UICollectionView works. Here is some code I tried:
I know its I mess, but at least I was trying ))
I ve tried this toget indexPath, but none works.
-(IBAction)deletePressed:(UIButton* )sender{
RecipeCollectionHeaderView *contentView = (RecipeCollectionHeaderView *)[sender superview];
RecipeViewCell *cell = (RecipeViewCell *)[contentView superview];
cell = (RecipeViewCell *)[contentView superview];
// determine indexpath for a specific cell in a uicollectionview
NSIndexPath *editPath = [self.collectionView indexPathForCell:cell];
NSInteger rowIndex = editPath.row;
NSInteger secIndex = editPath.section;
// NSIndexPath *indexPath = nil;
// indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
// NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// NSSet *touches = [event allTouches];
//
// UITouch *touch = [touches anyObject];
//
// CGPoint currentTouchPosition = [touch locationInView:self.collectionView];
//
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: currentTouchPosition];
NSLog(#"IndexPath CollectionView %ld", (long)rowIndex);
}
I also tried make it work like in this post, but it does not give a result:how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView
Thank you all!
EDIT!
I found this works well:
-(IBAction)deletePressed:(UIButton* )sender{
[self.ordersArray removeObjectAtIndex:0];
[self.collectionView reloadData];
}
The only thing I need now is to get section of UICollectionView by UIBotton that been pressed, to delete section I want. How to do it?)
You also need to remove item from the array and UICollectionView as well.
Try to use this code.
-(void) deleteRow:(NSInteger)index {
[self.contentView performBatchUpdates:^{
[arrayForRows removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.contentView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
For cell selection use this code snippet:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//Item position that you would like to delete.
indexPath.row;
}
This question already has answers here:
indexPathForCell returns nil since ios7
(3 answers)
Closed 9 years ago.
Before asking this question, I searched a lot on Google and Stackoverflow. Tried also some examples, but I can't make the function work.
Since the hierarchy of the tableview is changed since iOS 7, is it kind of hard to find a solution.
I got a standard tableview with a couple items and one button on the screen.
I need to get the indexPath.row number when selecting an item from the tableview and clicking on the button.
This is my code
- (IBAction)buttonGetNumber:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[(UIView *)[button superview] superview]];
NSLog(#"%i", indexPath.row);
}
This keeps returning a '0', no matter which item I select from the tableview.
I also tried this (2):
- (IBAction)buttonGetNumber:(id)sender {
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *index = [self.tableView indexPathForCell:cell];
NSLog(#"%i", index.row);
}
This also returns a '0'.
I also tried this (3):
- (IBAction)buttonGetNumber:(id)sender {
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[[senderButton superview] superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(#"%i", rowOfTheCell);
}
And this makes the application crash.
Any clue how I can solve this?
Create an instance variable _lastClickedRow Set it with tableview delegate like below. And when you click the to "Get Row" button use _lastClickedRow.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_lastClickedRow = indexPath.row;
}
- (IBAction)buttonGetNumber:(id)sender {
NSLog(#"%d" , _lastClickedRow);
}
If for some reason selected cell doesn't work for you (e.g. for a multiple selection case), you could get row from sender's frame:
- (IBAction)buttonGetNumber:(id)sender
{
CGPoint buttonOrigin = sender.frame.origin;
CGPoint pointInTableview = [self.tableView convertPoint:buttonOrigin fromView:sender.superview];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableview];
if (indexPath) {
// Do your work
}
}
You simply set the tag of the UIButton same as the indexPath.row using:
yourButton.tag = indexPath.row;
in didSelectRowForIndexPath:.
Then in buttonGetNumber: method, you get the row number using:
int rowNum = [(UIButton*)sender tag];
Here, you have the advantage of not using any third variable.