TextField in UITableView
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开发者_如何学JAVA 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{
...
}
don't know if it's a typo in the question or in the code:
[self.viewWithTag:1]
but you shouldn't have a dot in there:
[self viewWithTag:1]
Your code for the save method should read:
-(IBAction ) saveChanges:(id) sender
{
UITextField *name =(UITextField *)[self.view viewWithTag:1];
UITextField *bday= (UITextField *)[self.view viewWithTag:2];
UITextField *note=(UITextField *)[self.view viewWithTag:3];
//// some code ////////////
}
You are accessing a tagged subview of the view relating to the controller in which this code is placed.
精彩评论