这个问题很简单。对于在第 0 节和第 1 节中具有相同标题的每一行,如何使 tableView 中的附件视图被“选中”?(iOS)
我想这样做是因为当我连续选择附件视图时,该行会复制到一个新的第 0 部分,该部分称为“收藏夹”。但问题是,如果我取消选择第 0 部分中的行,该行会消失,但在第 1 部分中的相应行中,附件视图仍处于选中状态,在这种情况下我不想被取消选择。提前致谢。
-(void)addToFavs:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
UIButton *button = (UIButton*)sender;
if(indexPath!=nil){
if(button.selected==YES){
button.selected = NO;
}else{
button.selected =YES;
}
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor=[UIColor lightGrayColor];
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
cell.textLabel.text=self.favs[indexPath.row];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}
//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 25, 25);
button.frame = frame;
button.showsTouchWhenHighlighted = YES;
[button setImage:[UIImage imageNamed:@"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"favorites.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
if(indexPath.section==0){
button.selected = !button.selected;
}
cell.accessoryView.tag=indexPath.row;
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
return cell;
}