获取索引路径如果用户在UITableview中编辑UITextfield

时间:2015-07-24 10:58:26

标签: ios objective-c uitableview

我正在使用下面的UITableview图像。如果我输入单价和数量,我需要计算。但现在我不知道如何在UITableView中获取两个文本框的indexpath。在UITableView中,如果按钮单击转到didSelectRowAtIndexPath方法。我在输入qty和unitprice文本框时没有调用相同的方法。

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>{

4 个答案:

答案 0 :(得分:2)

你可以用各种方法做到这一点

输入-1

// get the current visible rows

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

2型

您可以获得您触摸的单元格

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

3型

 add `TapGestuure ` for get the current cell.

类型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];
}

例如

如果您需要其他信息,请参阅此link

答案 1 :(得分:1)

试试这个

#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

答案 2 :(得分:0)

在textfield委托方法中,您可以获取行的索引路径,也可以直接获取文本字段文本

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

答案 3 :(得分:0)

对您的UITableViewCell进行子类化。使用protocol等方法为每个单元格创建cellName:(UITableViewCell*)cellName didSelectItem:(Item*) item。您可以找到更多信息here

UITableViewCellUITextView代表设为self,并实施protocoltextFieldShouldReturn:方法会指示用户何时输入内容,因此您将在其中拨打[self.delegate cellName:self didEditText:textField.text]

MainViewController tableView: cellForRowAtIndexPath:设置UITabelViewCell个代表,并实施protocols

因此,当用户输入任何文本字段时,您将在MainViewController中知道,与表格行号无关。