uitableviewcontroller中的自定义文本字段

时间:2011-02-03 10:11:53

标签: iphone

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

在此代码中还有一些文本字段。

我的问题是,当我滚动文本字段中的表值时会自动删除.. 任何人都可以帮助我???

1 个答案:

答案 0 :(得分:0)

要在单元格中添加textField,请创建自己的UITableViewCell,如this

更新:

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