I am using SKSTableView control.I have added button on cell.I have facing one issue When i clicked on button then i got wrong row and subrow.
- (IBAction)btnPickListPressed:(id)sender
{
UIButton *btn=sender;
PicklistTableViewCell *cell = (PicklistTableViewCell *)btn.superview.superview;
NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
NSLog("%#",indexPath.row);
NSLog("%#"indexPath.subRow);
}
First copy below code and open SKSTableView.h
- (NSIndexPath *)correspondingIndexPathForRowAtIndexPath:(NSIndexPath *)indexPath
paste this below to #interface SKSTableView : UITableView in SKSTableView.h
and replace your action method code to this:
UIButton *btn=sender;
PicklistTableViewCell *cell = (PicklistTableViewCell *)btn.superview.superview;
NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
NSIndexPath *correspondingIndexPath = [self.tblView correspondingIndexPathForRowAtIndexPath:indexPath];
NSLog("%#",correspondingIndexPath.row);
NSLog("%#"correspondingIndexPath.subRow);
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 made a UITableViewCell nib with a button on it. When the button is pressed, I want to delete the cell. The table view isn't in editing mode and I'm not using a standard UITableViewCell delete button.
I could store the row number in the button tag from cellForRowAtIndexPath, and use that to determine the row to delete, but when a cell is deleted, the button tags will be incorrect.
Any ideas how I can identify what button press relates to what row?
Like in this answer:
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
If your button is a subview of cell's content view (as it should be). You can get the index path for the cell with button like this:
UIView *cellContentView = yourButton.superview;
UITableViewCell *cell = cellContentView.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Then, just delete the cell:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
if the method the button calls has a signature like:
-(void)action:(id)sender;
sender will be the UIButton that called the action so:
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = [[button superview] superview];
should get you what you want.
I am using a tableView which loads a custom UITableViewCell with a "Tap" button inside it. A method is called when user clicks the button.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...
[btnRowTap addTarget:self action:#selector(didButtonTouchUpInside:) forControlEvents:UIControlEventTouchDown];
...
return cell;
}
In the didButtonTouchUpInside method, Im trying to retrieve the value of row selected in the following way:
-(IBAction)didButtonTouchUpInside:(id)sender{
UIButton *btn = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)btn.superview;
NSIndexPath *indexPath = [matchingCustTable indexPathForCell:cell];
NSLog(#"%d",indexPath.row);
}
The problem is that on clicking button at any row, I am getting the same value of 0 every time.
Where am I going wrong?
You must NOT rely on the view hierarchy of a UITableViewCell. This approach will fail in iOS7, because iOS7 changes the view hierarchy of the cell. There will be an additional view between your button and the UITableViewCell.
There are better ways to handle this.
Convert the button frame so it is relative to the tableview
ask the tableView for the indexPath at the origin of the new frame
.
-(IBAction)didButtonTouchUpInside:(id)sender{
UIButton *btn = (UIButton *) sender;
CGRect buttonFrameInTableView = [btn convertRect:btn.bounds toView:matchingCustTable];
NSIndexPath *indexPath = [matchingCustTable indexPathForRowAtPoint:buttonFrameInTableView.origin];
NSLog(#"%d",indexPath.row);
}
set Button tag in to cellForRowAtIndexPath method Befor setting Method like
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...
btnRowTap.tag=indexPath.row
[btnRowTap addTarget:self action:#selector(didButtonTouchUpInside:) forControlEvents:UIControlEventTouchDown];
...
return cell;
}
and your tapped cell getting like this:-
-(IBAction)didButtonTouchUpInside:(id)sender{
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
//Type cast it to CustomCell
UITableViewCell *cell = (UITableViewCell*)[tblView1 cellForRowAtIndexPath:indPath];
NSLog(#"%d",indPath.row);
}
try like this,if you are adding button to cell content view then use below code.
UITableViewCell *buttonCell = (UITableViewCell *)sender.superview.superview;
UITableView* table1 = (UITableView *)[buttonCell superview];
NSIndexPath* pathOfTheCell = [table1 indexPathForCell:buttonCell];
int rowOfTheCell = [pathOfTheCell row];
int sectionOfTheCell = [pathOfTheCell section];
btn.superview is contentView of UITableviewCell. Use btn.superview.superview instead.
if you already knew the value inside the cell, then
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *currentCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if ([currentCell.textLabel.text isEqualToString:#"Your Cell Text value" ]){
//Do theStuff here
}
}
here is the code for your ibAction.you dont need to set any tag or anything else
-(IBAction)didButtonTouchUpInside:(id)sender{
NSIndexPath *indexPath =
[tbl
indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
}
I've got dynamic tableView and one problem. In each tableCell are two buttons.
I'm trying to get indexPath.row on button touched but with no success.
My code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.homeTableView indexPathForSelectedRow];
NSLog(#"%d", indexPath.row);
}
How can I get indexPath.row for touced button (Segue connections are over this two buttons->two connections)?
It's not working because you do not select row when you click button.
With two buttons I would disconnect your segues from buttons and do 2 manual segues in IB then add code like that to handle them:
-(void)button1Pressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.homeTableView indexPathForCell:clickedCell];
...
[self performSegueWithIdentifier:#"YourManualSegueIdentifier1" sender:self];
}
Edit for second option:
In your MyTableViewCell.h:
#interface MyTableViewCell : UITableViewCell
...
#property (strong, nonatomic) NSIndexPath *cellIndexPath;
...
#end
In your MyTableViewCell.m:
-(void)button1Pressed:(id)sender {
...
NSInteger row = cellIndexPath.row;
...
}
In your MyTableViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// remember index here!
[cell setCellIndexPath:indexPath];
....
return cell;
}
Apple change the UITableViewCell hierarchy in iOS 7
Using iOS 6.1 SDK
<UITableViewCell>
| <UITableViewCellContentView>
| | <UILabel>
Using iOS 7 SDK
<UITableViewCell>
| <UITableViewCellScrollView>
| | <UITableViewCellContentView>
| | | <UILabel>
So if you want to get indexpath at button index then use following code
Using iOS 6 or later SDK
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
NSLog(#"index=%#",clickedButtonPath);
Using iOS 7 or 7+ SDK
UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
NSIndexPath *clickedButtonPath = [book_table indexPathForCell:clickedCell];
NSLog(#"index=%ld",(long)clickedButtonPath.item);
I am not using storyboard but once i did it in simple viewconroller based project, i think this may help you.
- (IBAction)goToNew:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) [btn superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
Maybe the touch happened to the UIButton instead of the UITableView / UITableViewCell.
Check with :
NSLog(#"%#",[sender class]);
Solved over button.tag
In "cellForRowAtIndexPath" I set button.tag = indexPath.row and then in "- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{"
UIButton *btn = (UIButton*) sender;
NSLog(#"%d", btn.tag);
give button tag = indexpath.row+100;
and get..
-(IBAction)ShowListView:(id)sender
{
UIButton *btn = (UIButton *)sender;
int Iindex = btn.tag-100;
NSLog(#"button title is %#",btn.titleLabel.text);
NSLog(#"button id is %d",Iindex);
NSLog(#"array category is....%#",arrayCategory);
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.