我在.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])
{
[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];
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"] ];
}
else{
[cell.btnSelection setImage:[UIImage imageNamed:@"uncheck"] ];
}
cell.lblQRcodeText.text=text;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
return cell;
}
这是我的工作