如何在Objective C中的"dynamic NSArray"中添加null 0r nill值

Muj*_*uju 11 arrays core-data objective-c uitableview ios

我正面临着增加Null or nil价值的问题NSArray.实际上我正在增加Null价值,因为我的数组计数不一样.我在Custom TableViewCell两个数组中添加三个数组来自webservice,一个数组来自数据库.我保存IndexPathcore data,然后检索它.

在此输入图像描述

如图所示,我indexPath将保存在String中并将其转换NSIntegerDidSelectAtIndexPath并显示在其中cellForRowAtIndexPath.我的问题是,它被覆盖,因为它存储在字符串中.所以我将它保存在coredata一个并检索它但是我遇到了数组不匹配计数的问题cellforrowatindexpath.我的代码是这样的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
         static NSString *STI=@"STI";
        AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
        cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
        cell.auditdesclbl.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];

        NSManagedObject *device2 = [devices objectAtIndex:indexPath.row];
        NSMutableArray *Checkarray=[devices valueForKey:@"Key"]; // Hear in this array I am getting Index count problem
        NSLog(@"Device =%@",device2);
        NSLog(@"Check Array =%@",Checkarray);
        if(indexPath.row == CurrentIndexPath)
        {
            cell.listlbl.text=GlobalString;
            [cell setBackgroundColor:[UIColor greenColor]];
        }

        return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
           CurrentIndexPath=indexPath.row;
            NSLog(@"Current Index Path =%ld",(long)CurrentIndexPath);
            GlobalIndexPath = [[NSString stringWithFormat: @"%ld", (long)CurrentIndexPath] mutableCopy];

            NSManagedObjectContext *context = [self managedObjectContext];

            if (self.device) {
                // Update existing device
                [device setValue:GlobalStringChk forKey:@"Key1"];
                [device setValue:GlobalIndexPath forKey:@"Key"];


            } else {
                // Create a new device
                NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
                [newDevice setValue:GlobalStringChk forKey:@"Key1"];
                [newDevice setValue:GlobalIndexPath forKey:@"Key"];
            }

            NSError *error = nil;
            // Save the object to persistent store
            if (![context save:&error]) {
                NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
            }



            [Audittable reloadData];



            NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
            self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码听到我节省了indexpathcoredata和阵列取回.我有一个数组计数的问题cellforrowatindexpath.如何Null or nil在数组中添加值以使其计数相同.我的数组是一个动态数组.主要的问题是,如果用户点击它,我需要更改单元格的颜色.当我将值存储在NSInteger并将其转换为索引路径时,我可以更改颜色但仅限于一个单元格time.When我点击其他单元格整数值得到覆盖.因此,我将它保存到核心数据并进行检索,但是当我在cellforrowatindexpath中获取核心数据数组时,由于计数不同而崩溃了.谢谢提前!

San*_*ani 7

我们不能nil直接在集合中添加,NSMutableArray因为它会引发异常.如果根本需要添加nil内部集合NSMutableArray,我们可以通过使用单例类来实现NSNull.

例如,我们有一个类型的数组NSMutableArray,我们可以nil使用NSNullas-

[array addObject:@"string"];
[array addObject:[NSNull null]];
Run Code Online (Sandbox Code Playgroud)

... 等等...

NSNull类定义用于表示单独的对象null中采集对象(不容许值nil的值).


Lio*_*ion 5

首先,您不能在 中添加元素NSArray,必须NSMutableArray改用。你不能nil在你的NSMutableArray.

所以,你应该添加empty string而不是nil像这样的东西,

NSMutableArray *arr = [[NSMutableArray alloc]init];

[arr addObject:@""];
Run Code Online (Sandbox Code Playgroud)

或者您可以添加NSNull对象,例如,

[arr addObject:[NSNull null]];
Run Code Online (Sandbox Code Playgroud)

并且您可以在想要以UI类似方式显示时检查字符串是否为空(如果添加了空字符串),

NSString *tempStr = [arr objectAtIndex:0];

if (tempStr.length > 0) {

    NSLog(@"string is not empty.");
}
else{

    NSLog(@"Sring is empty");
}
Run Code Online (Sandbox Code Playgroud)


Muj*_*uju 1

不需要在核心数据中保存索引Path,也不需要添加Null或nill值。

只需在 viewDidLoad 方法中分配 NSMutableArray

NSMutableArray *_selectedIndexPath = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)

然后在 didSelectRowatIndexPath addObject 数组中像这样

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        if (![_selectedIndexPath containsObject:indexPath]) { // Check if array does not contain selecting cell, add this cell to array
            [_selectedIndexPath addObject:indexPath];
            [tableView reloadData];
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后在Cellforrowatindexpath中

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       if ([_selectedIndexPath containsObject:indexPath]) {
                cell.backgroundColor=[UIColor greenColor];
            }
            else
            {
                cell.backgroundColor=[UIColor whiteColor];
            }
}
Run Code Online (Sandbox Code Playgroud)

您可以在不使用核心数据和 null 或 nil 值的情况下更改颜色。