2vision2
2vision2

Reputation: 5023

Get indexpath If user edit the UITextfield in UITableview

I'm using the UITableview like below image. If i typing Unit price and Qty i need to calculate. But now i dont know how to get indexpath for two text box in UITableView . In UITableView if button clicking goes to didSelectRowAtIndexPath method. Same method not calling when I typing in qty and unitprice textbox.

enter image description here

InvoiceMainViewController.h

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>

    @class InvoiceGridViewController;

    @protocol InvoiceTableCellProtocoll <NSObject>
    -(void) didPressButton:(InvoiceGridViewController *)theCell; 
    @end

    @interface InvoiceGridViewController : UITableViewCell {
             id<InvoiceTableCellProtocoll> delegationListener; }

    @property (nonatomic,assign) id<InvoiceTableCellProtocoll>  delegationListener;
    @property (nonatomic,retain) IBOutlet UITextField *invoiceItem;
    @property (nonatomic,retain) IBOutlet UITextField *invoiceUnitPrice;
    @property (nonatomic,retain) IBOutlet UITextField *invoiceQty;
    @property (nonatomic,retain) IBOutlet UITextField *invoiceTaxRate; 

    @property (nonatomic,retain) IBOutlet UILabel *invoiceItemId;
    @property (nonatomic,retain) IBOutlet UILabel *invoiceCurrencyId;
    @property (nonatomic,retain) IBOutlet UILabel *invoiceTaxRateId;

    @property (nonatomic,retain) IBOutlet UILabel *totalItemTax; @property
    @property (nonatomic,retain) IBOutlet UILabel *converstionMessage;

    -(IBAction)deleteSel:(id)sender;
    -(void)delFileSel; 
    @end

InvoiceMainViewController.m

#import <UIKit/UIKit.h>
#import "SBPickerSelector.h"
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperationManager.h"
#import "AFURLResponseSerialization.h"
#import "AFURLRequestSerialization.h"
#import "InvoiceGridViewController.h"


@interface InvoiceMainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,NSXMLParserDelegate,UITextFieldDelegate,InvoiceTableCellProtocoll>{

Upvotes: 0

Views: 513

Answers (4)

Nishant Gupta
Nishant Gupta

Reputation: 141

Try this one

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
{
   [super viewDidLoad];
   firstTxt.delegate = self;
   secondTxt.delegate = self;
  // Do any additional setup after loading the view, typically from a   nib.
 }

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textFieldShouldBeginEditing");
textField.backgroundColor = [UIColor colorWithRed:220.0f/255.0f    green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"textFieldShouldEndEditing");
textField.backgroundColor = [UIColor whiteColor];
return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
[tableView reloadData];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
#pragma mark -
#pragma mark - UITableView Delegates 
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


     UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"FirstTableViewCell"];

     if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"FirstTableViewCell"];
        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
if (![firstTxt.text  isEqual: @""] && ![secondTxt.text  isEqual: @""])
{
    NSString *fir = firstTxt.text;
    NSString * sec = secondTxt.text;
    cell.textLabel.text= [NSString stringWithFormat:@"%d",[fir intValue]+[sec intValue]];
}
else
{
       cell.textLabel.text=@"";
}

    //etc.

return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return 4;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//   NSLog(@"testing1");



}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.layer.backgroundColor = [UIColor clearColor].CGColor;
cell.backgroundColor = [UIColor clearColor];
}
@end






.h 


 //
 //  ViewController.h
 //  TestingText


 #import <UIKit/UIKit.h>

 @interface ViewController :        UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
 {

IBOutlet UITableView *tableView;
IBOutlet UITextField *secondTxt;
IBOutlet UITextField *firstTxt;
}


@end

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can do this in various methods

Type -1

// get the current visible rows

NSArray *paths = [yourtableName indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[paths objectAtIndex:0];

Type-2

you can get which cell you touched

 CGPoint touchPoint = [sender convertPoint:CGPointZero toView: yourtableName];
NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint: yourtableName];

Type-3

 add `TapGestuure ` for get the current cell.

Type-4

- (void) textFieldDidEndEditing:(UITextField *)textField {
CGRect location = [self convertRect:textField.frame toView:self.tableView];
NSIndexPath *indexPath = [[self.tableView indexPathsForRowsInRect:location] objectAtIndex:0];

// Save the contents of the text field into your array:
[yourArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
}

example

here if you need additional information see this link

Upvotes: 2

aramusss
aramusss

Reputation: 2401

Subclass your UITableViewCells. Create a protocol for each cell, with methods like cellName:(UITableViewCell*)cellName didSelectItem:(Item*) item. You can find more info here.

In the UITableViewCell set your UITextView delegate to self, and implement the protocol. textFieldShouldReturn: method, will indicate when the user is typing, so inside you will call [self.delegate cellName:self didEditText:textField.text].

In your MainViewController tableView: cellForRowAtIndexPath: set the UITabelViewCell's delegates, and implement the protocols.

So you will know in MainViewController when a user has typed in any of your textfields, independently from the table row number.

Upvotes: 0

poojathorat
poojathorat

Reputation: 1220

In textfield delegate method you can get the indexpath of row or you can directly get the textfields text

-(void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint hitPoint = [txtfield convertPoint:CGPointZero toView:tbl];
hitIndex = [tbl indexPathForRowAtPoint:hitPoint];
int localCount = [txtfield.text intValue];
}

Upvotes: 0

Related Questions