我有 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];
答案 0 :(得分:3)
我在.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];
}
}
in 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;
}
它为我工作