Search code examples
iosuitableviewcheckmark

First check mark hides when i started scrolling table


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];

return cell;
}

this is my code when ever i started scrolling the table towards downwards or upwards the first cell which i have checked have loosed the check mark sign, it is just hides all the way.. what is wrong with my code?


Solution

  • Reuse Cell ,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    
            int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            if(getIndex<21)
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        } // END HERE
        cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
        return cell;
    }