0

我正在制作一个 iphone 应用程序,我希望当应用程序加载时,当用户在第一个单元格中输入任何数据时,它应该只有一个带有文本字段的单元格,它应该是带有输入数据的新单元格,并且第一个单元格应该是空的,因为它是可编辑的。我正在制作,但它在上面的单元格中也显示了相同的值。

在此处输入图像描述

这是代码

           -(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {



    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];


   cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    cell.backgroundColor = [UIColor clearColor];

   }



   if (indexPath.section==0) {
    tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(13, 6, 250, 26)];
    tagInputField.tag = 2;
    tagInputField.delegate = self;
     tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    tagInputField.font=[UIFont fontWithName:@"Myriad-Pro" size:8];
    [tagInputField setText:@" "];

    tagInputField.textColor =[UIColor grayColor];

    [cell addSubview:tagInputField];

    cell.textLabel.text=@"";

    return cell;

    }

    if (indexPath.section==1) {
    UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(230, 8, 18, 18)];
    //crossButton.backgroundColor = [UIColor purpleColor];
    crossButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cross.png"]];
    [cell addSubview:crossButton];
    cell.textLabel.font =[UIFont systemFontOfSize:13];
    cell.textLabel.textColor =[UIColor grayColor];
    cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
    [crossButton addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];

    return cell;


    }        

    }
4

1 回答 1

0

试试这个代码:

-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.backgroundColor = [UIColor clearColor];
    }

    for (UIView *subView in cell.subviews)
    {
        if (subView.tag == 2 || subView.tag == 22) 
        {
            [subView removeFromSuperview];
        }
    }


    if(indexPath.section==0){
        tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(13, 6, 250, 26)];
        tagInputField.tag = 2;
        tagInputField.delegate = self;
        tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
        tagInputField.font=[UIFont fontWithName:@"Myriad-Pro" size:8];
        [tagInputField setText:@" "];
        tagInputField.textColor =[UIColor grayColor];
        [cell addSubview:tagInputField];
        cell.textLabel.text=@"";
        return cell;
    }

    if(indexPath.section==1) {
        UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(230, 8, 18, 18)];
        *crossButton.tag = 22; //use a tag value that is not used for any other subview
        //crossButton.backgroundColor = [UIColor purpleColor];
        crossButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cross.png"]];
        [cell addSubview:crossButton];
        cell.textLabel.font =[UIFont systemFontOfSize:13];
        cell.textLabel.textColor =[UIColor grayColor];
        cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
        [crossButton addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
于 2013-10-21T07:54:09.310 回答