UITableView中的TextField [英] TextField in UITableView

查看:124
本文介绍了UITableView中的TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   - (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
........ //// some code ........... ///// /////
CGRect frame = CGRectMake(5,10,320,44);
UITextField * txtField = [[UITextField alloc] initWithFrame:frame];
[txtField setBorderStyle:UITextBorderStyleNone];
txtField.delegate = self;
switch(indexPath.row){
case 0:
txtField.placeholder = editFrndBDb.frndName;
txtField.text = editFrndBDb.frndName;
txtField.tag = 1;
break;
case 1:
txtField.placeholder = editFrndBDb.bDay;
txtField.text = editFrndBDb.bDay;
txtField.tag = 2;
break;
case 2:
txtField.placeholder = editFrndBDb.frndNote;
txtField.text = editFrndBDb.frndNote;
txtField.tag = 3;
break;
默认值:
break;
}
[cell.contentView addSubview:txtField];
[txtField release];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

- (IBAction)saveChanges:(id)sender
{

UITextField * name =(UITextField *)[self.viewWithTag:1 ];
UITextField * bday =(UITextField *)[self.viewWithTag:2];

UITextField * note =(UITextField *)[self.viewWithTag:3];
////一些代码////////////

im使用此代码在tableview中显示文本字段,然后从文本字段访问值。获取错误在saveChange方法UITextField * name =(UITextField *)[self.viewWithTag:1]错误: - 视图是不是一个结构或联合。
退出此

解决方案

您的文本字段不是ViewController类视图的子视图自)。它在一个特殊的 UITAbleViewCell 内。所以你必须找出你想要的textview内容从哪个表格单元格,并从单元格的内容视图。



此外,你可以节省一些麻烦在未来,并检查您是否获得在请求时返回的视图,例如:

  UIView * aView = [someView viewWithTag:1] ; 
if(aView!= nil){
...
} else {
...
}


i m new to iphone ... trying this code but getting some error help me out..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
........//// some code ...........//////////
        CGRect frame = CGRectMake(5 ,10 , 320, 44);
    UITextField *txtField = [[UITextField alloc]initWithFrame:frame];
    [txtField setBorderStyle:UITextBorderStyleNone];
    txtField.delegate=self;
    switch (indexPath.row) {
        case 0:
            txtField.placeholder=editFrndBDb.frndName;
            txtField.text=editFrndBDb.frndName;
            txtField.tag=1;
            break;
        case 1:
            txtField.placeholder=editFrndBDb.bDay;
            txtField.text=editFrndBDb.bDay;
            txtField.tag=2;
            break;
        case 2:
            txtField.placeholder=editFrndBDb.frndNote;
            txtField.text=editFrndBDb.frndNote;
            txtField.tag=3;
            break;
        default:
            break;
    }
    [cell.contentView addSubview:txtField];
    [txtField release];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;    
}

-(IBAction ) saveChanges:(id) sender
{

    UITextField *name =(UITextField *)[self.viewWithTag:1];
    UITextField *bday= (UITextField *)[self.viewWithTag:2];

    UITextField *note=(UITextField *)[self.viewWithTag:3];
    ////  some code //////////// 

i m using this code to display textfield in tableview and then access the value from the textfield. Bbut getting the error in the "saveChange" method "UITextField *name =(UITextField *)[self.viewWithTag:1]" error :- view is something not a structure or a union. plz me out of this

解决方案

Your textfield is not a subview of the ViewController class's view (where you reference "self"). It's inside a particular UITAbleViewCell. So you'll have to figure out which table cell you want the textview contents from and get it out of the cell's contentview.

Moreover, you could save some trouble in the future and check that you get a view returned when asking for it, e.g.:

UIView *aView = [someView viewWithTag:1];
if( aView != nil ){
...
}else{
...
}

这篇关于UITableView中的TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆