我试过了:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
// this code im trying to toggle already ticked cell
NSString *knownObject = @"YES";
NSArray *alreadyTickedBoxes;
// NSInteger dictIndex= -1;
NSMutableDictionary *dict;
for (dict in self.dataArray){
// dictIndex++;
alreadyTickedBoxes = [dict allKeysForObject:knownObject];
if (alreadyTickedBoxes.count !=0)
break;
}
if(alreadyTickedBoxes.count != 0){
[dict setObject:[NSNumber numberWithBool:NO] forKey:@"checked"];
FeedCell3 *cellToUntick = [dict objectForKey:@"cell"];
UIButton *button = (UIButton *)cellToUntick.accessoryView;
UIImage *newImage = [UIImage imageNamed:@"unticked24"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
// this code toggles tapped cell
NSMutableDictionary *item = [self.dataArray objectAtIndex:indexPath.row] ;
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
FeedCell3 *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unticked24"] : [UIImage imageNamed:@"ticked24"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
[tableView reloadData];
}
答案 0 :(得分:0)
我通过保留对最后选择的索引的引用来实现它。
#pragma mark - UITableViewDelegate
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.feedTableView];
NSIndexPath *indexPath = [self.feedTableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.feedTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
// initialise previous index path to current index path
if(self.oldIndexPathRowIndex<0){
self.oldIndexPathRowIndex = indexPath.row;
}
// the following code should toggle previously selected row
if ( self.oldIndexPathRowIndex != indexPath.row) {
NSMutableDictionary *dictAtOldIndex = [self.dataArray objectAtIndex: self.oldIndexPathRowIndex];
BOOL checked = [[dictAtOldIndex objectForKey:@"checked"] boolValue];
NSString *text = [dictAtOldIndex objectForKey:@"text"] ;
NSLog(@"Toggling previously tapped row: %@ checked: %d", text, checked);
[dictAtOldIndex setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
checked = [[dictAtOldIndex objectForKey:@"checked"] boolValue];
NSLog(@"Previously tapped row: %@ toggled to: %d", text, checked);
FeedCell3 *cellToUntick = [dictAtOldIndex objectForKey:@"cell"];
UIButton *button = (UIButton *)cellToUntick.accessoryView;
UIImage *newImage = [UIImage imageNamed:@"unticked40x43"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
//update current index path
self.oldIndexPathRowIndex = indexPath.row;
}
// this code toggles currently tapped row
NSMutableDictionary *item = [self.dataArray objectAtIndex:indexPath.row] ;
BOOL checked2 = [[item objectForKey:@"checked"] boolValue];
NSString *text = [item valueForKey:@"text"];
NSLog(@"Toggling currently tapped row: %@ checked: %d", text, checked2);
[item setObject:[NSNumber numberWithBool:!checked2] forKey:@"checked"];
checked2 = [[item objectForKey:@"checked"] boolValue];
NSLog(@"Currently tapped row: %@ toggled to: %d", text, checked2);
FeedCell3 *cell = [item objectForKey:@"cell"];
UIButton *button2 = (UIButton *)cell.accessoryView;
UIImage *newImage2 = (checked2) ? [UIImage imageNamed:@"unticked40x43"] : [UIImage imageNamed:@"ticked40x43"];
[button2 setBackgroundImage:newImage2 forState:UIControlStateNormal];
// NSLog(@"Old Index Path is now row: %d", self.oldIndexPathRowIndex);
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self tableView: self.feedTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
[self.feedTableView deselectRowAtIndexPath:indexPath animated:YES];
}