代码之家  ›  专栏  ›  技术社区  ›  Stefan

向可变数组添加/删除对象

  •  0
  • Stefan  · 技术社区  · 9 年前

    我有 所有专业 阵列,以及 选定的专业 大堆我从服务器下载这些数组,将它们解析为对象,并将它们添加到这些数组中。现在,我想检查/取消检查一些专业。我试过了 containsObject ,但这不起作用,因为这些对象不在同一内存位置。这是我迄今为止完成的代码,它正在工作,但我有问题如何将它们添加到这个数组中。 在里面 cellForRowAtIndexPath :

    for (Speciality *specTemp in self.selectedSpecialities) {
            if (speciality.specialityId == specTemp.specialityId) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                break;
            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    

    在里面 didSelect :

    Speciality *speciality = [[Speciality alloc]init];
        speciality = self.allSpecialities[indexPath.row];
        NSMutableArray *tempSelectedSpecialities = [[NSMutableArray alloc]initWithArray:self.selectedSpecialities];
        int i=0;
        for (Speciality *tempSpeciality in tempSelectedSpecialities) {
            if (tempSpeciality.specialityId == speciality.specialityId) {
                [self.selectedSpecialities removeObjectAtIndex:i];
            }
            else {
    
            }
            i++;
        }
    
        [self.delegate companySpecialities:self.selectedSpecialities];
        [self.specialitiesTableView reloadData];
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   Bhumesh Purohit    9 年前

    我在.h中声明了一个Mutable数组来存储数据

     NSMutableArray *selectedMarks;
    

    在viewDidLoad中分配内存

    selectedMarks = [NSMutableArray new];
    

    在didSelectRowAtIndexPath中添加和删除对象

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       TableViewCell *cell = (TableViewCell *)[self.tblview cellForRowAtIndexPath:indexPath];
    
    
        NSString *strIndex=[NSString stringWithFormat:@"%ld",(long)indexPath.section];
    
        if ([selectedMarks containsObject:strIndex])// Is selected?
        {
            [selectedMarks removeObject:strIndex];
        }
        else{
            [selectedMarks addObject:strIndex];
            }
    }
    

    在单元格ForRowAtIndexPath中

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CRTableViewCellIdentifier = @"TableViewCell";
            TableViewCell *cell = (TableViewCell *)[self.tblview dequeueReusableCellWithIdentifier:CRTableViewCellIdentifier];
            if (cell == nil) {
                cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CRTableViewCellIdentifier];
            }
            cell.backgroundColor=[UIColor colorWithRed:122/255.0 green:196/255.0 blue:251/255.0 alpha:1];
    
            // Check if the cell is currently selected (marked)
               NSString *txtQRCodeid = [[[dictListQR objectForKey:@"boxlist"]objectAtIndex:indexPath.section]valueForKey:@"qr_code_id"];
    
    NSString *text1 = [[[dictListQR objectForKey:@"boxlist"]objectAtIndex:indexPath.section]valueForKey:@"box_name"];
            NSString *text=[NSString stringWithFormat:@"QR Code %ld with %@",indexPath.section+1,text1];
    
             cell.isSelected = [selectedQR containsObject:txtQRCodeid] ? YES : NO;
            if (cell.isSelected) {
                [cell.btnSelection setImage:[UIImage imageNamed:@"check"] ];
                // set image of selected
            }
            else{
                [cell.btnSelection setImage:[UIImage imageNamed:@"uncheck"] ];
                // set unselected image
            }
         cell.lblQRcodeText.text=text;
       cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
            return cell;
           }
    

    这是我的工作