iPhone-获取放置在自定义UITableViewCell中的UITextField值

iPhone-获取放置在自定义UITableViewCell中的UITextField值,iphone,objective-c,Iphone,Objective C,通常我有标签和一些文本字段,它们是IBOutlets,如果有人单击按钮,我就可以很容易地读取这些文本字段的文本值 self.myTextfield1.text; 但是现在我有了一个带有自定义表格单元格的UITableView。在这些单元格中,我有一个标签和一个文本字段。加载时,我在CellForRowatineXpath中执行此操作: - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath

通常我有标签和一些文本字段,它们是IBOutlets,如果有人单击按钮,我就可以很容易地读取这些文本字段的文本值

self.myTextfield1.text;
但是现在我有了一个带有自定义表格单元格的
UITableView
。在这些单元格中,我有一个标签和一个文本字段。加载时,我在
CellForRowatineXpath
中执行此操作:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = (MyCell*)[theTableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (!cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            cell = self.myCell;
            self.myCell = nil;
    }
    switch (indexPath.section) {
    case 0:
        switch (indexPath.row) {
            case 0:
                cell.myCellLabel.text = @"Label1";
                cell.myCellTextField.text = @"...";
                break;
            case 1:
                cell.myCellLabel.text = @"Label2";
                cell.myCellTextField.text = @"...";
                break;
            // and so on...
            default:
                break;
        }
        break;
    case 1:
        switch (indexPath.row) {
            case 0:
                cell.myCellLabel.text = @"Label1_1";
                cell.myCellTextField.text = @"...";
                break;
            // and so on...
        }
    default:
        break;
    }
    return cell;
}
因此,如果有人单击该按钮,我将无法访问带有
self.mytextfield1.text
的字段的文本值。如何获取字段的值?我知道标签的哪个部分和哪个行应该是哪个值。所以,如果我能结合节和行号得到文本字段的值,那就太好了

也许有人知道如何解决这个问题


最好的问候,Tim。

如果将文本字段的值存储在数组中,那么数组的索引将与行相同。您甚至可以有多个阵列:
cellLabelArray
cellTextFieldArray
这不是非常优雅,但可以解决问题。然后,您将摆脱switch语句并执行如下操作:

cell.myCellLabel.text = [cellLabelArray objectAtIndex:indexPath:row];
cell.myTextField.text = [cellTextFieldArray objectAtIndex:indexPath:row];
使用数组作为UITableView的数据源是一种常见的模式


我不知道当按下按钮时会发生什么,但是如果需要更改标签的值,只需更改数组中的值,然后向tableview发送一条消息。

如果将文本字段的值存储在数组中,那么数组的索引将与行相同。您甚至可以有多个阵列:
cellLabelArray
cellTextFieldArray
这不是非常优雅,但可以解决问题。然后,您将摆脱switch语句并执行如下操作:

cell.myCellLabel.text = [cellLabelArray objectAtIndex:indexPath:row];
cell.myTextField.text = [cellTextFieldArray objectAtIndex:indexPath:row];
使用数组作为UITableView的数据源是一种常见的模式


我不知道当按下按钮时会发生什么,但如果需要更改标签的值,只需更改数组中的值,然后向tableview发送一条
重新加载数据
消息。

这里的问题是表格单元格被重复使用,因此无法从单元格中“提取”输入的值,而是让单元格使用UITextFieldDelegate将输入的值“推送”给您

在自定义UITableViewCell中,为section和row添加变量,将单元格设置为textfield委托,并使用自定义委托协议将生成的textfield事件转发给viewcontroller以及附加的section和row。然后,让viewcontroller将传入数据写入UITableView数据源读取数据时的同一位置

下面是一个没有节但带有“类型”的示例:

#import <Foundation/Foundation.h>

@protocol ArticleTextFieldViewCellDelegate

-(void)didUpdateTextField:(int)index type:(int)type withText:(NSString*)text;

@end

@interface ArticleTextFieldViewCell : UITableViewCell <UITextFieldDelegate> {

    IBOutlet UITextField* valueTextField;

    id<ArticleTextFieldViewCellDelegate> delegate;

    int index;
    int type;
}

@property (nonatomic, retain) IBOutlet UITextField* valueTextField;
@property (nonatomic, retain) id<ArticleTextFieldViewCellDelegate> delegate;
@property (nonatomic, assign) int index;
@property (nonatomic, assign) int type;

@end
#导入
@协议ArticleTextFieldViewCellDelegate
-(void)didUpdateTextField:(int)索引类型:(int)type with text:(NSString*)text;
@结束
@接口ArticleTextFieldViewCell:UITableViewCell{
IBOutlet UITextField*valueTextField;
id代表;
整数指数;
int型;
}
@属性(非原子,保留)IBOutlet UITextField*valueTextField;
@属性(非原子,保留)id委托;
@属性(非原子,赋值)int索引;
@属性(非原子,赋值)int类型;
@结束
和实施

#import "ArticleTextFieldViewCell.h"
#import <UIKit/UIKit.h>

@implementation ArticleTextFieldViewCell

@synthesize type, valueTextField, delegate, index;

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(delegate) {
        [delegate didUpdateTextField:index type:type withText:[textField.text stringByReplacingCharactersInRange:range withString:string]];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    // optionally trigger delegate method here
}

@end
#导入“ArticleTextFieldViewCell.h”
#进口
@实现ArticleTextFieldViewCell
@综合类型、valueTextField、委托、索引;
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
如果(代表){
[delegate didUpdateTextField:索引类型:type withText:[textField.text StringByReplacingCharactersRange:range withString:string]];
}
返回YES;
}
-(BOOL)textField应返回:(UITextField*)textField{
[textField resignFirstResponder];
返回YES;
}
-(BOOL)textField应取消编辑:(UITextField*)textField{
返回YES;
}
-(void)textfielddidediting:(UITextField*)textField{
//这里可以选择触发委托方法
}
@结束

如果每个单元格有多个文本字段,请使用更多的委托方法或其他变量,如“type”。

这里的问题是表格单元格被重用,因此您不能从单元格中“提取”输入的值,而是让单元格使用UITextFieldDelegate将输入的值“推”给您

在自定义UITableViewCell中,为section和row添加变量,将单元格设置为textfield委托,并使用自定义委托协议将生成的textfield事件转发给viewcontroller以及附加的section和row。然后,让viewcontroller将传入数据写入UITableView数据源读取数据时的同一位置

下面是一个没有节但带有“类型”的示例:

#import <Foundation/Foundation.h>

@protocol ArticleTextFieldViewCellDelegate

-(void)didUpdateTextField:(int)index type:(int)type withText:(NSString*)text;

@end

@interface ArticleTextFieldViewCell : UITableViewCell <UITextFieldDelegate> {

    IBOutlet UITextField* valueTextField;

    id<ArticleTextFieldViewCellDelegate> delegate;

    int index;
    int type;
}

@property (nonatomic, retain) IBOutlet UITextField* valueTextField;
@property (nonatomic, retain) id<ArticleTextFieldViewCellDelegate> delegate;
@property (nonatomic, assign) int index;
@property (nonatomic, assign) int type;

@end
#导入
@协议ArticleTextFieldViewCellDelegate
-(void)didUpdateTextField:(int)索引类型:(int)type with text:(NSString*)text;
@结束
@接口ArticleTextFieldViewCell:UITableViewCell{
IBOutlet UITextField*valueTextField;
id代表;
整数指数;
int型;
}
@属性(非原子,保留)IBOutlet UITextField*valueTextField;
@属性(非原子,保留)id委托;
@属性(非原子,赋值)int索引;
@属性(非原子,赋值)int类型;
@结束
和实施

#import "ArticleTextFieldViewCell.h"
#import <UIKit/UIKit.h>

@implementation ArticleTextFieldViewCell

@synthesize type, valueTextField, delegate, index;

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(delegate) {
        [delegate didUpdateTextField:index type:type withText:[textField.text stringByReplacingCharactersInRange:range withString:string]];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    // optionally trigger delegate method here
}

@end
#导入“ArticleTextFieldViewCell.h”
#进口
@实现ArticleTextFieldViewCell
@综合类型、valueTextField、委托、索引;
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
如果(代表){
[delegate didUpdateTextField:索引类型:type withText:[textField.text StringByReplacingCharactersRange:range withString:string]];
}
返回YES;
}
-(BOOL)textField应返回:(UITextField*)textField{
[textField resignFirstResponder];
返回YES;
}
-(BOOL)textField应取消编辑:(UITextField*)textField{
返回YES;
}
-(void)textField