iOS UITableViewCellAccessoryCheckmark Visible ob every scroll

后端 未结 4 1155
甜味超标
甜味超标 2021-01-28 02:50

I have a list which I have using as a check boxes. I have enable or disable Check mark on row on select. But when I scroll the list its make mark row after every 10 rows.

<
相关标签:
4条回答
  • 2021-01-28 03:01

    Its because UITableView reuses the cell. So, in the method cellForRowAtIndexPath, you will have to check for a particular cell (of a particular section and row), if it needs to be checked on, provide the accessory type.

    If not needed for that cell, provide accessory type as none.

    0 讨论(0)
  • 2021-01-28 03:03
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    
        if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    
            [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
            [NSMutableArray addObject:[AnotherMutableArray objectAtIndex:indexPath.row]];
    
        } else {
    
            [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    
           [NSMutableArray removeObject:[AnotherMutableArray objectAtIndex:indexPath.row]];
    
        }
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    

    Also in your viewDidLoad, instantiate you both mutable arrays-

    yourmutableArray1 = [[NSMutableArray alloc]init];
    yourmutableArray2 = [[NSMutableArray alloc]init];
    
    0 讨论(0)
  • 2021-01-28 03:04

    UItableView reuses the cell in every scroll so using condition as per accessory type is not a good practice. You can Create an NSMutableArray with selected items and Check as per the Condition below.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        if ([selected containsIndex:indexPath.row]) {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        } else {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        // Do the rest of your code
        return cell;
    } 
    

    in didSelectrowAtindexpath method you can Add and remove the Selected items.

    0 讨论(0)
  • 2021-01-28 03:19

    You need to put your logic to set accessory type for cell in cellForRowAtIndexPath, and to identify the cell to mark with check mark you can mark the object in the list in didSelectRowAtIndexPath: or manage an array of selected/unselected objects of the list here.

    0 讨论(0)
提交回复
热议问题