簡體   English   中英

如何從位於自定義UITableView中的UITextField讀取數據

[英]How to read data from UITextField located in custom UITableView

首先,我知道還有其他幾本相同的書名。 我已經檢查了它們,但是我要問的是一個特定的問題。

我想做什么:創建包含電子郵件和密碼的登錄表。 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返回類型默認為id。

看到我做錯了嗎?

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

您的ViewController是繼承UIView還是UITableViewController? 假設您在與Table View相同的控制器上調用tableView。

Yu不能在數據源和委托方法之外使用tableView。 接口定義中的表視圖的名稱是什么? 用那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM