【问题标题】:How to read data from UITextField located in custom UITableView如何从位于自定义 UITableView 中的 UITextField 读取数据
【发布时间】:2012-02-21 15:13:55
【问题描述】:

首先我知道还有几个标题看起来一样。我已经检查过了,但我要问的是一个特定的问题。

我想要做什么: 创建了包含电子邮件和密码的登录表。 UITableView 中的 UITextfields。只是想将这些数据放入一些 NSString 变量指针中以进行进一步处理。

我有一个名为 CustomCell 的自定义单元类,如下所示:

#import "CustomCell.h"
#import "QuartzCore/QuartzCore.h"

@implementation CustomCell
@synthesize textfield;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        textfield = [[UITextField alloc] initWithFrame:CGRectMake(5, 7, 160, 20)];
        textfield.textColor = [UIColor blackColor];
        textfield.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:textfield];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:NO animated:NO];
    // Configure the view for the selected state.
}

- (void)dealloc {
    [super dealloc];
}
@end

我有 ViewController,我使用下面的自定义表格,您可能会看到我的 tableView cellForRowAtIndex 方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    [tableView.layer setCornerRadius:10.0];

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}

        switch (indexPath.row) {
            case 0:
                cell.textfield.placeholder = @"Email";
                break;
            case 1:
                cell.textfield.placeholder = @"Password";
                cell.textfield.tag = 0;
                break;
    }

    return cell;
}

最后在同一个班级下面我正在尝试阅读电子邮件和密码

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // ERROR!!!
NSString *userpassword = cell.textfield.text;

错误:未知接收器类型tableView:

注意在同一行:
ClassMethod +cellforrowatindexpath not found 返回类型默认为id。

你看到我做错了吗?

【问题讨论】:

  • 请忽略tableView cellForRawAtIndexPath 方法中的标签。
  • 为什么忽略标签?在这里使用标签实际上并不是一个坏主意。标记您的电子邮件文本字段 1 和密码文本字段 2。让您的视图控制器成为这两个文本字段的代表并实现 - (BOOL)textFieldShouldReturn:(UITextField *)textField。在此方法中读取标签以区分哪个文本字段结束了编辑。获取值并将其保存在控制器 ivar 中。我没有输入这个作为答案,因为我认为您问题的真正答案应该是如果您不需要显示表格数据,请不要使用表格视图

标签: ios uitableview nsstring uitextfield


【解决方案1】:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; // Add This instead
NSString *userpassword = cell.textfield.text;

【讨论】:

    【解决方案2】:

    你不能在数据源和委托方法之外使用 tableView。您的界面定义中的表格视图的名称是什么?使用那个。

    【讨论】:

      【解决方案3】:

      你的 ViewController 是继承 UIView 还是 UITableViewController?这是假设您在与 Table View 相同的控制器上调用 tableView。

      【讨论】:

        猜你喜欢
        • 2018-01-06
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多