【问题标题】:How to Clear Text in UITextField of CustomCell of UITableview?如何清除 UITableview 的 CustomCell 的 UITextField 中的文本?
【发布时间】:2012-11-01 21:09:11
【问题描述】:

我在 UITableview 的 CustomCell 中有一个 UITextField,我正在使用这个 UITableview 作为 Mainview 的子视图。我的 MainView 上还有另一个 UITableview,但我使用这个 UITableview 作为我的 MainView 的子类。下面的图像也将是帮助理解我的问题。

如图所示,UITableview 上方是我的子类 UITableviw,下方是单行 UITableview,其中 CustomCell 具有 UITextField。现在我希望当我单击子类 UITableview 的任何单元格时,我的 UITextField 文本是清晰的,我在 UITableview 的 CustomCell 中拥有。

注意:-为了让我的问题更容易,我有一些代码可以在我的 MainView 上(不在 UITableview 的 CustomCell 中)有 UITextfield 时对我有用。这是我的代码。 在 Mainview.h 文件中

#import <UIKit/UIKit.h>
#import "Inputtableview.h"
@interface testingViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITextField *txtfinput;
Inputtableview *inputview;
IBOutlet UITableView *inputtbl; 
}
@property (nonatomic, retain) UITextField *txtfinput;
@end

然后在 Mainview.m 文件的 ViewDidload 中

- (void)viewDidLoad {
if (inputview == nil) {
    inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
    inputview.view = inputview.tableView;
    inputview.myAccessToMaintxtf = txtfinput;
[super viewDidLoad];
}

现在在我的 UITableview.h 文件的子类中

#import <UIKit/UIKit.h>
@interface Inputtableview : UITableViewController<UITableViewDelegate,UITableViewDataSource> {
}
@property (nonatomic, assign) UITextField *myAccessToMaintxtf;
@end

最后在我的子类 UITableview.m 文件中

@implementation Inputtableview
@synthesize myAccessToMaintxtf; 


#pragma mark -
#pragma mark Table view delegate

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

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
 myAccessToMaintxtf.text=@"";
 }

正如我之前提到的,当我的 MainView 中有 TextField 时,上面的代码可以正常工作,但是当我想清除 UITableview 的 CustomCell 中的 UITextField 的文本时,我不知道如何修复它。任何帮助都将被应用。Thanx提前。

【问题讨论】:

    标签: iphone xcode uitableview uitextfield subclass


    【解决方案1】:

    好的,我看不到您如何将“myAccessToMaintxtf”连接到 CustomCell 中的文本字段。我用来从我的自定义单元格中获取子视图的是“标签”。标签可以用作子视图的标识符。因此,如果您使用的是 xib,则可以在文本字段的设置选项卡中设置标签,如果您通过代码创建文本字段,则可以将标签设置为:

     myAccessToMaintxtf.tag = 11; //11 is and example number, use the number that you want
    

    然后,当您想访问您的文本字段时,请使用:

    theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:11];
    

    您可以在 UIView 类参考中找到有关标签的更多信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

    祝你好运

    编辑 2:

    如果您使用两个表格,并且您想清除一个表格中的文本字段,选择另一个表格,我认为您需要一些东西来在一个表格中选择的 indexPath 和单元格的 indexPath 之间建立链接文本字段。如果你使用类似的东西:

    table 1, indexpath:0,0 for celcius; 表 1,indexpath:0,1 为华氏温度; 表1,开尔文的indexpath:0,2;

    table 2, indexpath:0,0 for celcius; 表 2,indexpath:0,1 for farenheit; 表2,开尔文的indexpath:0,2;

    您只需使用相同的 indexPath 从其他表视图(输入表视图)获取带有文本字段的单元格,例如:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *cellWithTheFieldToClear = [yourInputTableView cellForRowAtIndexPath:indexPath];
        theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:2];
        theTextFieldToClear.text = @"";
    }
    

    【讨论】:

    • 我认为你的想法是对的,但我面临的问题就在这里,请看pastebin.ca/2251239
    • 您的 InputTableView 中只有(并且将永远有)一行?
    • 如果您将在 InputTableView 中有更多行...您将始终使用相同的行顺序?比如:华氏,第 1 行;开尔文,第 2 行;
    • 感谢您帮助我,所以只有我遇到的一个问题请检查pastebin.ca/2251300
    • 好的,我在这里缺少一些东西。哪个对象(或多个对象)是输入表的数据源?
    【解决方案2】:

    您是否将 myAccessToMaintxtf 添加为 dataSource cellForRowAtIndexPath 中 UITableViewCell 的子视图?如果你这样做了,你仍然必须使用 [tableView reloadCellAtIndexPath:...] 重新加载单元格;

    或者另一个建议是子类化 UITableViewCell,并添加一个 Outlet 属性,将 outlet 连接到 TextField。然后你可以做类似 selectedCell.textField.text = @"";在你 didSelectRowAtIndexPath 中。

    【讨论】:

    • 感谢您的快速回复,但我没有理解您的解释。如果您在这里使用我的代码进行解释,那就更好了。
    • 好的,很抱歉造成混乱。我今晚晚些时候再做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多