1

基本上我有这个 UItableview,它在生成时由 2 个单元组成:一个带有分数,如果你触摸另一个,它会插入另一行分数。所有这一切都很好,但是当我滚动并且第一个单元格离开屏幕时,当它回来时它会在它上面添加另一个单元格(它叠加它)!我想我做错了什么dequeueReusableCellWithIdentifieraddSubview但我无法弄清楚它是什么......

这是我的代码:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";


    if (feedIndex == 0)// configures the first cell

    {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier1];

    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, (44 - 15) / 2.0, 80, 15)];
    textField1.font = [UIFont boldSystemFontOfSize:15];
        textField1.tag = i+1;// to keep track of the cells that are created by the user
        i++;
    textField1.textAlignment = UITextAlignmentCenter;
        [textField1 addTarget:self//to get rid of the keyboard
                        action:@selector(saisieReturn:)
              forControlEvents:UIControlEventEditingDidEndOnExit];
    textField1.text = @"00";
            [cell.contentView addSubview:textField1];


    textField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, (44 - 15) / 2.0, 80, 15)];
    textField2.font = [UIFont boldSystemFontOfSize:15];
                textField2.tag = i+1;
        i++;
    textField2.textAlignment = UITextAlignmentCenter;
        [textField2 addTarget:self
                      action:@selector(saisieReturn:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
        textField2.text = @"00";
    [cell.contentView addSubview:textField2];


    textField3 = [[UITextField alloc] initWithFrame:CGRectMake(160, (44 - 15) / 2.0, 80, 15)];
    textField3.font = [UIFont boldSystemFontOfSize:15];
                textField3.tag = i+1;
        i++;
    textField3.textAlignment = UITextAlignmentCenter;
        [textField3 addTarget:self
                      action:@selector(saisieReturn:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
        textField3.text = @"00";
        [cell.contentView addSubview:textField3];


    textField4 = [[UITextField alloc] initWithFrame:CGRectMake(240, (44 - 15) / 2.0, 80, 15)];
    textField4.font = [UIFont boldSystemFontOfSize:15];
                textField4.tag = i+1;
        i++;
    textField4.textAlignment = UITextAlignmentCenter;
        [textField4 addTarget:self
                      action:@selector(saisieReturn:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
        textField4.text = @"00";
        [cell.contentView addSubview:textField4];
        }
            return cell;

    }

    else if (feedIndex == nbrow -1) //configures the second cell
    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
        if(cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        }
        cell.textLabel.text = @"Add score";

        return cell;
    }

    else // configures all of the other cells that are created by the user when he clicks and the second cell
    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];

        textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, (44 - 15) / 2.0, 80, 15)];
        textField1.font = [UIFont boldSystemFontOfSize:15];
        textField1.tag = i+1;
        i++;
        textField1.textAlignment = UITextAlignmentCenter;
        [textField1 addTarget:self//to get rid of the keyboard
                       action:@selector(saisieReturn:)
             forControlEvents:UIControlEventEditingDidEndOnExit];
        textField1.text = @"00";
        [cell.contentView addSubview:textField1];


        textField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, (44 - 15) / 2.0, 80, 15)];
        textField2.font = [UIFont boldSystemFontOfSize:15];
        textField2.tag = i+1;
        i++;
        textField2.textAlignment = UITextAlignmentCenter;
        [textField2 addTarget:self
                       action:@selector(saisieReturn:)
             forControlEvents:UIControlEventEditingDidEndOnExit];
        textField2.text = @"00";
        [cell.contentView addSubview:textField2];


        textField3 = [[UITextField alloc] initWithFrame:CGRectMake(160, (44 - 15) / 2.0, 80, 15)];
        textField3.font = [UIFont boldSystemFontOfSize:15];
        textField3.tag = i+1;
        i++;
        textField3.textAlignment = UITextAlignmentCenter;
        [textField3 addTarget:self
                       action:@selector(saisieReturn:)
             forControlEvents:UIControlEventEditingDidEndOnExit];
        textField3.text = @"00";
        [cell.contentView addSubview:textField3];


        textField4 = [[UITextField alloc] initWithFrame:CGRectMake(240, (44 - 15) / 2.0, 80, 15)];
        textField4.font = [UIFont boldSystemFontOfSize:15];
        textField4.tag = i+1;
        i++;
        textField4.textAlignment = UITextAlignmentCenter;
        [textField4 addTarget:self
                       action:@selector(saisieReturn:)
             forControlEvents:UIControlEventEditingDidEndOnExit];
        textField4.text = @"00";
        [cell.contentView addSubview:textField4];

        return cell;            
    }}

我是 iOS 开发新手,我在编写第一个应用程序时遇到了第一个问题!我一直在寻找整个互联网,但即使我找到了有类似问题的线程,我也无法解决我的问题。

4

1 回答 1

6

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];

textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, (44 - 15) / 2.0, 80, 15)];
textField1.font = [UIFont boldSystemFontOfSize:15];
textField1.tag = i+1;
i++;
textField1.textAlignment = UITextAlignmentCenter;
[textField1 addTarget:self//to get rid of the keyboard
               action:@selector(saisieReturn:)
     forControlEvents:UIControlEventEditingDidEndOnExit];
textField1.text = @"00";
[cell.contentView addSubview:textField1];


textField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, (44 - 15) / 2.0, 80, 15)];
textField2.font = [UIFont boldSystemFontOfSize:15];
textField2.tag = i+1;
i++;
textField2.textAlignment = UITextAlignmentCenter;
[textField2 addTarget:self
               action:@selector(saisieReturn:)
     forControlEvents:UIControlEventEditingDidEndOnExit];
textField2.text = @"00";
[cell.contentView addSubview:textField2];


textField3 = [[UITextField alloc] initWithFrame:CGRectMake(160, (44 - 15) / 2.0, 80, 15)];
textField3.font = [UIFont boldSystemFontOfSize:15];
textField3.tag = i+1;
i++;
textField3.textAlignment = UITextAlignmentCenter;
[textField3 addTarget:self
               action:@selector(saisieReturn:)
     forControlEvents:UIControlEventEditingDidEndOnExit];
textField3.text = @"00";
[cell.contentView addSubview:textField3];


textField4 = [[UITextField alloc] initWithFrame:CGRectMake(240, (44 - 15) / 2.0, 80, 15)];
textField4.font = [UIFont boldSystemFontOfSize:15];
textField4.tag = i+1;
i++;
textField4.textAlignment = UITextAlignmentCenter;
[textField4 addTarget:self
               action:@selector(saisieReturn:)
     forControlEvents:UIControlEventEditingDidEndOnExit];
textField4.text = @"00";
[cell.contentView addSubview:textField4];

return cell;            
于 2012-11-24T06:30:35.370 回答