简体   繁体   中英

How to read data from UITextField located in custom UITableView

First of all I know there are several more titles looks like same. I have checked them but what I ask is a specific problem.

What I want to do: Created Login table which includes email and password. UITextfields in UITableView. Just want to take those data into some NSString variable pointers for further process.

I have a Custom Cell Class named CustomCell, as below:

#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

And I have ViewController where I use the custom table below you may see my tableView cellForRowAtIndex method.

-(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;
}

And lastly below in same class I am trying to read email & password with

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

Error: Unknown receiver type tableView:

Attention in same line:
ClassMethod +cellforrowatindexpath not found return type defaults to id.

Did you see what I am doing wrong?

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

Is your ViewController inheriting UIView or UITableViewController? This is assuming you are calling tableView on the same controller as the Table View.

Yu can't use tableView outside the datasource and delegate methods. What's the name of your tableview in your interface definition? Use that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM