0

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>{
Cœur
  • 37,241
  • 25
  • 195
  • 267
2vision2
  • 4,933
  • 16
  • 83
  • 164

4 Answers4

2

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

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
Nishant Gupta
  • 131
  • 15
  • But i have created cell in separate class . here how i will handle? – 2vision2 Jul 24 '15 at 13:21
  • Sorry I didn't get you, are you using custom cell – Nishant Gupta Jul 25 '15 at 10:24
  • Yes i'm using UITableViewCells for different class and mapped with UITableViewcontroller – 2vision2 Jul 27 '15 at 05:51
  • no problem change FirstTableViewCell to your custom cell identifier – Nishant Gupta Jul 27 '15 at 06:44
  • I added UITextFieldDelegate in Mainviewcontroller. But textFieldDidBeginEditing and other method are working in UITableViewCells class only. But there i counld't find index path in textFieldDidBeginEditing and other method. – 2vision2 Jul 27 '15 at 07:58
  • Why are you use methods in uitableviewcell only use its xib and table delegates use in mainview controller – Nishant Gupta Jul 27 '15 at 08:00
  • i have declared that delegate in Mainviewcontroller only. i have put textFieldDidBeginEditing and etc method in both class. but i calling only inside of uitableviewcell class – 2vision2 Jul 27 '15 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84352/discussion-between-nishant-and-2vision2). – Nishant Gupta Jul 27 '15 at 09:48
  • Thanks i have fixed the issue using the following example https://github.com/chrismcclelland/ELTextFieldTableViewCell – 2vision2 Jul 28 '15 at 12:30
0

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];
}
poojathorat
  • 1,200
  • 2
  • 9
  • 19
0

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.

Community
  • 1
  • 1
aramusss
  • 2,391
  • 20
  • 30