【问题标题】:Adding/removing object to mutable array向可变数组添加/删除对象
【发布时间】:2016-01-12 09:41:10
【问题描述】:

我有 allSpecialities 数组和 selectedSpecialities 数组。我正在从服务器下载这些数组,将它们解析为对象并将它们添加到这些数组中。现在,我想选中/取消选中一些专业。我试过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];

【问题讨论】:

  • 初学者错误:在 didSelect 中,您创建了一个 Specialty 对象,然后立即将其覆盖。不要那样做。
  • 你能解释一下如何以其他方式做到这一点吗?
  • Speciality *speciality = self.allSpecialities[indexPath.row]; 像这样。
  • 好的,但这并没有解决我的问题。
  • 如果你想让containsObject:工作,你需要实现isEqual:,我认为,hash

标签: ios objective-c nsmutablearray


【解决方案1】:

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

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

在 cellForRowAtIndexPath 中

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

对我有用

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多