the question is quite simple. How can you make an accessoryView in a tableView to be "Selected" for each row that has the same title in both section 0 and 1? (iOS)
I want to do this because when I select the accessoryView in a row, the row copies up to a new section 0 which is called "favorites". But the problem is, if I deselect the row in section 0, the row disappears but the accessoryView stays selected for the corresponding row in section 1, which I wan't to be deselected in that case. Thanks in advance.
-(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;
}
Easy way to do this, create an array for favorite and add item to this array on select. And select accessory view if the item is in array of favorites.