2014-01-15 57 views
0

我尝试这样做:当新的单元格勾选(独占uitableviewcell选择)(ios)时,我如何解开勾选的单元格?

- (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

你有尝试'deselectRowAtIndexPath:'tableview的方法,而不是重新加载tableview。 ? –

+0

还没有。谢谢生病尝试 – jsky

+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]; 


} 
相关问题