UItableView Cell Resuablity数据在滚动时进行交互

时间:2014-01-23 09:19:48

标签: ios iphone uitableview reuseidentifier

Grouped TableView with 4 rows in each Section

大家好,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
        textField.tag = 123;
        textField.placeholder = @"Enter Text";
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.delegate = self;
        [cell.contentView addSubview:textField];

    }

    UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
        if (indexPath.row == 0) {
            [textField setPlaceholder:@"Employee ID"];
        }
        else if (indexPath.row == 1)
        {
            [textField setPlaceholder:@"Employee Name"];
        }
        else if (indexPath.row == 2)
        {
            [textField setPlaceholder:@"Employee Phone"];
        }
         if (indexPath.row == 3)
        {
            [textField setPlaceholder:@"Employee Email"];
            UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [saveButton setFrame:CGRectMake(200, 0, 100, 40)];
            [saveButton setTitle:@"Save Emp" forState:UIControlStateNormal];
            [saveButton addTarget:self action:@selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
            [saveButton setTag:indexPath.section];
            [cell addSubview:saveButton];
        }

    return cell;
}

大家好, 我正在使用这段代码来获得上述输出 但是当我滚动tableview时,我得到的输出是

when i am scrolling the tableview the output i am getting

如果我在该部分中输入任何文本并滚动文本,则文本在单元格中会发生变化。

8 个答案:

答案 0 :(得分:1)

您正在其中一个案例中向该单元格添加子视图:

[cell addSubview:saveButton];

当您将旧单元格出列时,不会删除此子视图。您必须明确删除这些情况的子视图。这将导致意外行为。

我真的建议继承UITableViewCell并将组件添加到该子类。通过这样做,您可以隐藏其saveButton属性,以用于不需要saveButton的情况。

答案 1 :(得分:1)

中使用标记初始化您的UITextFieldUIButton
if (cell == nil) 
{
    //Initialize your `UITextField` and `UIButton`
    // also set tag
}

UITextField语句中设置UIButtonif框架(您可以通过标记获取UITextFieldUIButton 确定它适合你。

答案 2 :(得分:1)

我也有同样的问题,试试这个,

UITableViewCell *cell;

if (cell == nil) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//在外面初始化tableviewcell,

答案 3 :(得分:0)

您需要使用custom UITableViewCell&在该类文件中,您需要将其绑定在单元类文件中。 更重要的是need to maintain the datasource for it

不要将tag用作0.我的意思是,为每个视图设置类似的内容

txtView.tag = 25+indexPath.section;

答案 4 :(得分:0)

在所有地方将此“textField.tag = 123”更改为“text.tag = indexPath.row”。它会正常工作。

答案 5 :(得分:0)

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

         UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
         textField.tag = 123;
         textField.placeholder = @"Enter Text";
         textField.autocorrectionType = UITextAutocorrectionTypeNo;
         textField.delegate = self;
         [cell.contentView addSubview:textField];
    } else {
         //here the cell is reused, so remove the button here
         [[cell.contentView viewWithTag:124] removeFromSuperview];
    }

    UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
    if (indexPath.row == 0) {
        [textField setPlaceholder:@"Employee ID"];
    }
    else if (indexPath.row == 1)
    {
        [textField setPlaceholder:@"Employee Name"];
    }
    else if (indexPath.row == 2)
    {
        [textField setPlaceholder:@"Employee Phone"];
    }
     if (indexPath.row == 3)
    {
        [textField setPlaceholder:@"Employee Email"];
        UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [saveButton setFrame:CGRectMake(200, 0, 100, 40)];
        [saveButton setTag:124];
        [saveButton setTitle:@"Save Emp" forState:UIControlStateNormal];
        [saveButton addTarget:self action:@selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
        //[saveButton setTag:indexPath.section];
        [cell.contentView addSubview:saveButton];
    }

    return cell;

}

答案 6 :(得分:0)

根据您的屏幕截图,有三个可见的单元格。因此,当您加载第4个单元格ios时,将为您提供第1个单元格的参考。在这里,您已经设置了员工电子邮件按钮。这样它就可以看到,你又可以设置员工ID和电话。

因此,当您重复使用单元格时,您需要将单元格重置为默认情况,在此示例中,您可以删除该按钮或隐藏它,然后根据单元格信息设置单元格属性。

我希望你能理解这个问题。

    if (indexPath.row == 3)
                {
                    [textField setPlaceholder:@"Employee Email"]; 
                    UIButton *saveButton = [cell.contentView viewWithTag:124];

                   if(saveButton == nil) 
                         saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];


                    [saveButton setFrame:CGRectMake(200, 0, 100, 40)];
                    [saveButton setTag:124];
                    [saveButton setTitle:@"Save Emp" forState:UIControlStateNormal];
                    [saveButton addTarget:self action:@selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
                    //[saveButton setTag:indexPath.section];
                    [cell.contentView addSubview:saveButton];
                }

答案 7 :(得分:0)

尝试使用此代码,在表格视图中重复使用单元格

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
            textField.tag = 123;
            textField.placeholder = @"Enter Text";
            textField.autocorrectionType = UITextAutocorrectionTypeNo;
            textField.delegate = self;
            [cell.contentView addSubview:textField];

           UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
           [saveButton setFrame:CGRectMake(200, 0, 100, 40)];
           [saveButton setTitle:@"Save Emp" forState:UIControlStateNormal];
           [saveButton addTarget:self action:@selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
           [saveButton setTag:124];
           [cell.contentView addSubview:saveButton];

        }

        UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
        UIButton *saveButton =(UIButton*)[cell.contentView viewWithTag:124];
       saveButton.hidden=YES;
            if (indexPath.row == 0) {
                [textField setPlaceholder:@"Employee ID"];
            }
            else if (indexPath.row == 1)
            {
                [textField setPlaceholder:@"Employee Name"];
            }
            else if (indexPath.row == 2)
            {
                [textField setPlaceholder:@"Employee Phone"];
            }
             if (indexPath.row == 3)
            {
                [textField setPlaceholder:@"Employee Email"];
                saveButton.hidden=NO;
            }

        return cell;
    }