how to get indexpath uitextfield inside tableviewcell? - ios

i just know to get indexpath button inside tableviewcell like this
CGPoint center= [sender center]; CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; NSLog(#"%#",indexPath);
but i need know about textfield thx for helping me guys!

There is another method to get the index row!
In cellForRow do this:
textField.tag = indexPath.row;
In the textField method do this:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSInteger row = textField.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
}

To get the indexPath of the cell in which the textfield is contained you need to do a couple of things.
Make the viewController class the delegate of the textField in the tableView's cell
Implement textFieldDidBeginEditing method of textField's delegateProtocol in your viewController's class like this
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//Here you can you the same logic to find the indexPath of the cell like you use in case of a button.
}

Related

I need to access the title of a selected row of a Table View outside the TableViewDelegate

I need to access the title of a selected row of a Table View.
But the issue arises when i need to access it outside the TableViewDelegate, i.e. : cellForRowAtIndexPath, didSelectRowAtIndexPath, etc.
Is there anyone who could help me.
Thanks in advance.
How about this ?
- (IBAction)ButtonAction:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *selectedindex = [tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *Cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
}

How delete row form UICollectionView?

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;
}

How can I use a button in a cell (placed with StoryBoard)

I'm searching for a way to use a button in a cell. I've created it in the prototype cell of my table view but i can't get to detect the cell it's in when I tap on it.
Can you help me please ?
Suppose this IBAction is the event handler for when you tap on the button:
- (IBAction) buttonTapped: (UIButton *) sender
{
//get parent cell
UITableViewCell *cell = [sender findSuperViewWithClass:[UITableViewCell class]];
//do whatever you want...
}
where you'll need this category:
#implementation UIView (SuperView)
- (UIView *)findSuperViewWithClass:(Class)superViewClass
{
UIView *superView = self.superview;
UIView *foundSuperView = nil;
while (nil != superView && nil == foundSuperView) {
if ([superView isKindOfClass:superViewClass]) {
foundSuperView = superView;
break;
} else {
superView = superView.superview;
}
}
return foundSuperView;
}
#end
"The way I handle buttons inside custom cells:
I define an IBAction that I connect to a UIButton event inside the custom cell
I define a delegate and a delegate method for the cell to handle the button action
The IBAction calls that delegate method
When defining your cells in cellAtRow... I set the tableViewController to be the delegate. cell.delegate=self;
I do whatever action I would like to do inside that delegate method in the tableViewController
"
from JP Hribovsek's answer of Handle button click inside UITableViewCell
- (void)buttonOnCellTapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.table];
NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonPosition];
if (indexPath)
{
NSLog(#"BUTTON TAPPED ON SECTION: %d, ROW: %d", indexPath.section, indexPath.row);
UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
}
}

How to get UITableView's row and section by UIButton tag?

There is UITableViewCell which contain UIButton.
In my cellForRowAtIndexPath:
cell.toCartBtn.tag = indexPath.row;
[cell.toCartBtn addTarget:self
action:#selector(toCart:) forControlEvents:UIControlEventTouchDown];
In toCart method I need to get row and section by tapped button tag. How can I do it?
See this code
- (void)toCart:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath)
{
...
}
}
Use this simple code if UIButton is subview in cell
- (void)toCart:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *cell=(UITableViewCell*)senderButton.superView;
NSIndexPath *index = [tableView indexPathForCell:cell];
}
Create a custom UITableViewCell subclass and handle the touch in there.
Then define a delegate for your custom table view cell and when you create the cell, assign your table view data source as the delegate.
When the cell handles the touch, send a message to the delegate and then find the index path from the table view.
-(void)customTableViewCellButtonPressed:(CustomTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
...
}
try this:
- (void)toCart:(id)sender {
CGPoint button1 = [sender convertPoint:CGPointZero toView:self.tblName];
NSIndexPath *index = [self.tableView indexPathForRowAtPoint:button1];
//use index variable
}
and also see for more details for your bug...
Detecting which UIButton was pressed in a UITableView
For Swift
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to: self.tableViewItems)
let indexPath = self.tableViewItems.indexPathForRow(at: buttonPosition)
if (indexPath != nil)
{
print(indexPath?.section)
print(indexPath?.row)
}

How to make a button on a table view cell call the correct tableView:cellForRowAtIndexPath?

I have a button that is part of a custom UITableViewCell. What I want is that when someone clicks the button, that the proper tableView:cellForRowAtIndexPath to be called for that cell.
How do I tie the button to the proper cellForRowAtIndexPath?
Any suggestions and / or code samples will be highly appreciated
You can get the indexPath from the touch event. Modify your buttonTapped method to be this:
- (void) buttonTapped:(id) sender forEvent:(UIEvent *)event
and then use something like the following:
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];
If you have only one section, you can probably use UIView's tag property.
Something like follows would theoretically work.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)idxPath
{
Your_UITableViewCell_Subclass * cell = [tableView dequeueCellWithIdentifier:#"..."];
cell.button.tag = idxPath.row;
....
return cell;
}
....
- (void) buttonTapped:(id) sender
{
int idx = ((UIButton *)sender).tag;
NSIndexPath * idxPath = [NSIndexPath indexPathForRow:idx inSection:0];
UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:idxPath];
// Do whatever you want to do with the cell
}

Resources