如何在自定义ViewCell中访问文本字段数据

时间:2011-02-10 23:31:41

标签: iphone uitableview

我的自定义视图单元格如下http://img34.imageshack.us/i/screenshot20110210at627.png/

@interface AddSiteAddressCell : UITableViewCell {
    IBOutlet UITextField *street;
    IBOutlet UITextField *city;
    IBOutlet UITextField *province;
    IBOutlet UITextField *postal;
    IBOutlet UITextField *country;
    IBOutlet UITextField *siteName;

}
@property (nonatomic,retain)UITextField *siteName;
@end


@implementation AddSiteAddressCell

- (void) textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"%@",street);    
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state.
}


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


@end

AddSiteViewController.m

-

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.section == 0)
    {
        static NSString *MyIndentifier = @"MyIdentifier";
        MyIndentifier = @"tblCellView";
        //static NSString *CellIdentifier = @"Cell";

        AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];

        if(cell == nil) {

            [[NSBundle mainBundle] loadNibNamed:@"AddSiteAddressCell" owner:self options:nil];
            cell = nameCell;
        }
        return cell;


    }else if (indexPath.section == 1)
    {
    static NSString *MyIndentifier = @"MyIdentifier";
    MyIndentifier = @"tblCellView";
    //static NSString *CellIdentifier = @"Cell";

    AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];

    if(cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"AddSiteAddressCell" owner:self options:nil];
        cell = addressCell;
    }
        return cell;
    }
    /*static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...*/

    return nil;
}





    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // Navigation logic may go here. Create and push another view controller.
        AddSiteAddressCell *cell =  (AddSiteAddressCell *)[add_site cellForRowAtIndexPath:[indexPath row]];                                                   
        self.test = cell.siteName.text;
        NSLog(@"%@",self.test);
    }

我的全景视图如下:

http://img29.imageshack.us/i/screenshot20110211at940.png/ 这就是我的View的样子。 2个部分,每个部分包含1行,每个部分包含1个自定义单元

来自我的CustomTableViewCell的代码

我想知道如何在表控制器中访问我的文本字段以保存输入的数据

1 个答案:

答案 0 :(得分:1)

你什么时候想要访问它? 你应该为你的文本字段(例如siteName)创建属性,然后如果你有你的单元格的IndexPath,你可以使用:

AddSiteAddressCell *myCell=(AddSiteAddressCell *)[tableView cellForRowAtIndexPath:MyIndexPath];
myCell.siteName....