Iphone uitableviewcontroller中的自定义文本字段
在这段代码中还有一些文本字段 我的问题是,当我滚动表格时,文本字段中的值会自动删除。。Iphone uitableviewcontroller中的自定义文本字段,iphone,Iphone,在这段代码中还有一些文本字段 我的问题是,当我滚动表格时,文本字段中的值会自动删除。。 有人能帮我吗?要在单元格中添加文本字段,请创建自己的UITableViewCell 更新: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //static NSString *CellIdentifier = @"Cell"; NS
有人能帮我吗?要在单元格中添加文本字段,请创建自己的UITableViewCell 更新:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
NSString *CellIdentifier = [NSStringstringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:@"First Name "];
Name.tag=kViewTag;
[cell.contentView addSubview:Name];
//cell.textLabel.text=@"First Name";
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 30)];
textName.placeholder=@"Enter First Name";
textName.tag=kViewTag;
[textName setBorderStyle:UITextBorderStyleRoundedRect];
//textName.tag=0;
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
textName.clearsOnBeginEditing=YES;
[cell.contentView addSubview:textName];
}
}
return cell;
}
如果不创建新单元格,我就无法进行此操作??是的,但最好使用您自己的UITableViewCell。我使用了自己的uitableviecell,但它也有相同的问题……滚动后,上面的行是清理值:x将textfields值存储在数组中,并在textfielddidediting方法中将它们放入cellForRow中相应的textField中。我已经用代码更新了我的答案。谢谢MathieuF。但是我的单元格现在没有清除值。我什么都没做p
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"YourCellId";
YourCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [YourCell cellFromNib];
}
cell.yourTextField.tag = indexPath.row;
cell.yourTextField.text = (NSString*)[self.textFieldValues objectAtIndex:indexPath.row];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.textFieldValues replaceObjectAtIndex:textField.tag withObject:textField.text];
}