由于网络原因看不到效果,我的是左边是一个大分类的cell,右边是一个小分类的cell,小分类的支持多选然后传给上一个界面,这里传值可以用block
其实说到底就是cell增加一个手势,用这个手势判断是不是选中状态,当点击cell的时候,判断cell上面的数据(这里我是定义了一个可变数组selectArray,一进入这个页面开始加载数据时这个数组就初始化,并用数组初始化,初始化的个数为父类tableview的数组个数,在定义一个int行数据,只要点击了父类tableview就记录这个cell,如果不记录其他子类tableview相同位置也会出现选中状态)当点击的时候,如果selectArray里面没有当前cell上的数据,就追加,如果有就删除,indexPath是cell里面记录哪段哪行的,如(0-0)
我都是在.m文件中操作的
int lastSelectBigTableViewRow;
@property(nonatomic,strong)NSMutableArray * bigDataArray;
@property(nonatomic,strong)NSMutableArray * smallDataArray;
@property(nonatomic,strong)NSMutableArray * selectArray;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [self.smallDataArray[indexPath.row] objectForKey:@"category_name"];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.textColor = [UIColor grayColor];
if ([[self.selectArray objectAtIndex:lastSelectBigTableViewRow] containsObject:indexPath]) {
cell.imageView.image = [UIImage imageNamed:@"cur-photo-blue.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:@"cur.png"];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}
}
return nil;
}
- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
CGPoint tapLocation = [tapRecognizer locationInView:_subTableView];
NSIndexPath *tappedIndexPath = [_subTableView indexPathForRowAtPoint:tapLocation];
if ([[self.selectArray objectAtIndex:lastSelectBigTableViewRow] containsObject:tappedIndexPath]) {
[[self.selectArray objectAtIndex:lastSelectBigTableViewRow] removeObject:tappedIndexPath];
}
else {
[[self.selectArray objectAtIndex:lastSelectBigTableViewRow] addObject:tappedIndexPath];
}
[_subTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
//这个是一个保存按钮,是我要取subView上的值传给上一个界面
- (void)saveClick{
//self.selectArray
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i < [self.selectArray count]; i++) {
if ([[self.selectArray objectAtIndex:i] count] > 0) {
//[str appendString:[[self.bigDataArray objectAtIndex:i] objectForKey:@"category_name"]];
for (int j = 0; j < [[self.selectArray objectAtIndex:i] count]; j++) {
NSIndexPath * path = [[self.selectArray objectAtIndex:i] objectAtIndex:j];
NSMutableArray * tempArray = [self.bigDataArray[i] objectForKey:@"items"];
NSDictionary * dic = [tempArray objectAtIndex:[path row]];
[str appendString:[dic objectForKey:@"category_name"]];
[str appendString:@","];
}
}
}
if (self.myBlock) {
self.myBlock(str);
}
[self.navigationController popViewControllerAnimated:YES];
}
参考:http://stackoverflow.com/questions/3666629/how-to-add-checkboxes-to-uitableviewcell