UITableViewCellAccessory Disappears When Scrolled Off Screen

前端 未结 5 520
独厮守ぢ
独厮守ぢ 2021-01-22 12:42

I have a UITableView filled with objects. In the didSelectRowAtIndexPath method i have a UITableViewCellAccessoryCheckmark appear when the row is selected and disappea

5条回答
  •  失恋的感觉
    2021-01-22 13:26

    This is what I find to work best:

    (just any code between "Add this" and "End Add this")

    Also, make sure you change "(Your Number ofRows)" to an object that either returns an int, or is an int itself. Such as myCustomArray.count, or 24.

    //In .h file add this
    NSMutableArray *indexArray;
    
    //in .m file 
    
    - (void)viewDidLoad {
        //Add this
        indexArray = [[NSMutableArray alloc] init];
        int i;
        for (i = 0; i <= (Your Number ofRows); i++) {
            [indexArray addObject:[NSNumber numberWithDouble:0]];
        }
    NSLog(@"indexArray = %@",indexArray);//You can check the console now to see if you have an array of "zeros" 
        //End Add this
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath];
    
    
    if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        [curCell setAccessoryType:UITableViewCellAccessoryNone];
        compareCount = (compareCount - 1);
    
        //Add this
        [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:0]]; 
        //End Add this
    
    
        if (tableView == [[self searchDisplayController] searchResultsTableView]) {
            NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
            [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]];
            [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]];
        }
        else {
            [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]];
            [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]];
        }
    
    }
    else {
        [curCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        compareCount = (compareCount + 1);
    
        //Add this
        [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:1]]; 
        //End Add this
    
        if (tableView == [[self searchDisplayController] searchResultsTableView]) {
            NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
            [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]];
            [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]];
        }
        else {
            [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]];
            [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]];
        }
    
    }
    
    if (compareCount > 0) {
        if (compareOn == YES){
        }
        else {
            compareButton.enabled = YES;
            UIImage *image = [UIImage imageNamed:@"redbutton.png"];
            [compareButton setImage:image];
        }
    }
    
    else {
        compareButton.enabled = NO;
        [compareButton setImage:nil];
        [compareButton setCustomView:nil];
    }
    
    }    
    
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *CellIdentifier = @"CustomCell";
    
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
    
    // Setting separate tables correctly....
    
    //Add this
    if ([[indexArray objectAtIndex:indexPath.row] doubleValue] == 1) {
        cell.accesoryType = UITableViewCellAccesoryTypeCheckmark;
    } else {
        cell.accesoryType = UITableViewCellAccesoryTypeNone; 
    }
    //End Add this 
    
    return cell;
    }
    

提交回复
热议问题