0

分组表视图,每个部分有 4 行

大家好,

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

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

当我滚动表格视图时,我得到的输出

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

4

8 回答 8

1

我也遇到了同样的问题,试试这个

UITableViewCell *cell;

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

//初始化外面的tableviewcell,

于 2014-01-23T10:56:55.780 回答
1

标签初始化你的UITextFieldUIButton

if (cell == nil) 
{
    //Initialize your `UITextField` and `UIButton`
    // also set tag
}

并设置框架和在声明之外。(你可以得到它的标签确保 它为你工作。UITextFieldUIButtonifUITextFieldUIButton

于 2014-01-23T09:49:37.213 回答
1

在其中一种情况下,您正在向单元格添加子视图:

[cell addSubview:saveButton];

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

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

于 2014-01-23T09:27:29.607 回答
0

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

不要使用tagas 0。我的意思是,为每个视图设置类似的东西

txtView.tag = 25+indexPath.section;

于 2014-01-23T09:27:05.680 回答
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];
                }
于 2014-01-23T09:37:35.873 回答
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;

}
于 2014-01-23T09:34:04.883 回答
0

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

于 2014-01-23T09:29:39.097 回答
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;
    }
于 2014-01-23T10:32:20.707 回答