ios - 当我添加一个新部分(通过步进器)时,一个部分中单元格的内容与另一个部分的内容混合

标签 ios objective-c uitableview

我有一个带有动态单元格的 tableView。每个部分有 5 行。每行的前 4 行都有一个文本字段。相反,在第五行有一个 imageview(当我点击这一行时,我可以从我的照片库中选择一张照片,最后一张将放在 imageView 中)。部分的数量是在运行时使用步进器决定的。第 0 部分是固定的,包含一个步进器。当我单击 + 按钮(在步进器上)时,将添加一个部分。所以一切都是正确的,但是如果我之前在包含文本字段的行中写入,然后添加一个或多个部分,这些文本字段的内容会相互混合(以及部分之间)。

//In file.h I've declared @property (nonatomic) int numberOfComponents;

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 + self.numberOfComponents;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return 1;
    }
    else{
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSString *CellIdentifier = @"cellStepper";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        UILabel *stepperLabel = (UILabel *)[cell viewWithTag:1];
        stepperLabel.text = [NSString stringWithFormat:@"%i",self.numberOfComponents];
        UIStepper *stepper = (UIStepper *)[cell viewWithTag:2];
        stepper.minimumValue = 0;
        stepper.maximumValue = 20;
        stepper.stepValue = 1;
        stepper.autorepeat = YES;
        stepper.continuous = YES;
        return cell;
    }

    if (indexPath.row == 4) {
        NSString *CellIdentifier = @"cellProfileSnap";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        return cell;
    }

    NSString *CellIdentifier = @"cellDetail";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UITextField *cellLabelComponent = (UITextField *)[cell viewWithTag:3];
    cellLabelComponent.placeholder = @"detail";
    return cell;
}

- (IBAction)stepperClik:(UIStepper *)stepper{
    if (stepper.value == 0 && self.numberOfComponents == 0) {
        if (stepper.value > self.numberOfComponents) {
            self.numberOfComponents += 1;

        }
        else{
            return;
        }
    }


    if (stepper.value > self.numberOfComponents) {
        self.numberOfComponents += 1;

    }
    else{
        self.numberOfComponents -= 1;

    }


    [self.tableView reloadData];

}

enter image description here

enter image description here

由 Greg 解决,但现在还有另一个问题:我有一个保存按钮,它应该将许多数组保存到字典中(包含每个部分 5 的详细信息)作为部分的数量。 这里是保存按钮的代码:

- (IBAction)saveButton:(id)sender{

    NSMutableArray *arrComponents = [[NSMutableArray alloc] init];
for (int i = 0; i < self.numberOfComponents; i++)
{
    NSMutableArray *component = [[NSMutableArray alloc] init];
    for (int j = 0; j < [self.arrDetails count]; j++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i+1];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
        if (j == 4){
            UIImageView *imageComponent = (UIImageView *) [cell viewWithTag:4];
            NSLog(@"%@",imageComponent.image);
            if (imageComponent.image == Nil) {
                [component addObject: nil];
            }
            [component addObject: imageComponent.image];
        }
        else{
            UITextField *detailComponent = (UITextField *) [cell viewWithTag:3];
            NSLog(@"%@",detailComponent.text);
            if ([detailComponent.text isEqualToString:@""]) {
                [component addObject:@""];
            }
            if (detailComponent.text != nil && i != 0)
                [component addObject: detailComponent.text];
        }

    }
    [arrComponents addObject: component];
    NSLog(@"%@",arrComponents);
}

在代码中显示的位置//ERROR HERE,在最新迭代(读取的最后一节)的 5 次迭代(一节中的行数)的第四次迭代中,应用程序崩溃并显示此消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

最佳答案

我相信您的 detailComponent 等于 nil,您不能将 nil 保存到数组中。在你打电话之前

[component addObject: detailComponent.text]; //ERROR HERE

做检查:

if (detailComponent != nil && i != 0)
    [component addObject: detailComponent.text]; 

这可能会发生,因为在您的第 0 节中您没有任何文本字段。

//已编辑

问题发生在这里:

if (imageComponent.image == Nil) {
    [component addObject: nil];
}
[component addObject: imageComponent.image];

替换为:

if (imageComponent.image == nil) {
    [component addObject:[NSNull null]];
}
else {
    [component addObject: imageComponent.image];
}

如果你想添加 nil,你不能将 nil 添加到数组中,你应该添加对象 NSNull。 而你缺少 else 声明。您的代码第一次尝试在您的 if 语句 ([component addObject: nil];) 中将 nil 添加到数组中两次(如果图像为 nil),第二次是在您的 if 语句之后:[component addObject: imageComponent.image];

//已编辑

按照下面代码注释中的建议进行更改。我使用相同的字典,您应该更改名称,因为它表明它只保留文本字段值,但现在它也会存储图像:

    if (indexPath.row == 4) {
        NSString *CellIdentifier = @"cellProfileSnap";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //I believe this is where you keep your imageView
        // get your image 
        UIImageView *imageComponent = (UIImageView *) [cell viewWithTag:4];
        NSString *key = [NSString stringWithFormat:@"sec:%d,row:%d", indexPath.section, indexPath.row];
        if (self.textValueDictionary[key])
            imageComponent.image = self.textValueDictionary[key];
        else
            imageComponent.image = nil; //Your default value (probably you want to set up your plus image)
        return cell;
    }

您需要做的下一步是在用户按下加号图像的位置 - 您必须将图像保存在字典中。将此代码放入您从用户那里获取图像的方法。这是一个伪代码,所以你必须稍微调整一下:

// Get reference to the cell
    UITableViewCell *cell = //have a look on textFieldDidEndEditing  method. You have to work out how to get the cell (I cannot do that because I haven't got access to your code);
    // Get index path
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];

    // Get section as a string
    NSString *key = [NSString stringWithFormat:@"sec:%d,row:%d", indexPath.section, indexPath.row];
    UIImage *img = //get your image here;
    //make sure it's not nil before you save it to dictionary
    //save it to dictionary
    [self.textValueDictionary setValue:img forKey:key];

最后一点是 saveButton: 方法 我昨天的建议有误。您正在尝试直接从单元格中获取文本和图像,如果单元格可见,它就可以正常工作。如果您看不到单元系统,将其放入可重用池中,就会得到 nils。 要修复它,您必须从字典中获取数据: 不要使用:

  UIImageView *imageComponent = (UIImageView *) [cell viewWithTag:4];

改为:

NSString *key = [NSString stringWithFormat:@"sec:%d,row:%d", indexPath.section, indexPath.row];
    UIImage *img = self.textValueDictionary[key];
    // make sure is not nil before you save it. If it's nil add [NSNull nil] to your array as I explained before.

当你阅读文本时不要那样做:

UITextField *detailComponent = (UITextField *) [cell viewWithTag:3];

从字典中获取文本:

     NSString *key = [NSString stringWithFormat:@"sec:%d,row:%d", indexPath.section, indexPath.row];
        NSString *str = self.textValueDictionary[key];

如果它提示数据类型使用强制转换 (UIImage*)。 请记住将您的第一个循环更改为:for (int i = 1; i < self.numberOfComponents; i++) 并且当您获得 NSIndexPath 时将其更改为 inSection:i 而不是 inSection:i+1。 我没有在 Xcode 中运行它,所以可能有一些错误,但你应该能够找到并修复它。 希望它对您有用。

关于ios - 当我添加一个新部分(通过步进器)时,一个部分中单元格的内容与另一个部分的内容混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226716/

相关文章:

ios - 如何隐藏特定 View Controller 中的状态栏?

ios - ObjectiveRecord nil模型错误

objective-c - 检查 NSString 是否包含有效数字但已本地化

swift - SwipeCellKit 删除 tableView 中的最后一行会产生错误

ios - CorePlot - 调用 barTouchUpAtRecordIndex 后更改 x 轴标签 :

android - 如何在Flutter中将CircularProgressIndicator放入另一个CircleProgressIndicator中?

ios - ViewControllers 未针对 3.5 英寸屏幕调整大小

ios - 删除 iOS 中的数组元素问题?

swift - 如何在 UITableViewCell 的 @IBAction 函数中推送 ViewController

ios - UITableView + 可触摸的UIView/UIScrollView behin