IPhone UITableViewCellAccessoryCheckMark

I had a problem creating CheckMarkAccessoryViewin UITableView. When I select a row in a table, I can get a marked label for this row, but randomly some other rows in the table also get a checkmark. I only want this cell to get a checkmark. How can i do this? I am coding an exclusive list. Below is the code I used.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{   
    if(tableView==tableView1)
    {
        return […arraycount1…. ];
    }
    else 
    {   
        return […arraycount2…. ];
    }
}

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

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    else 
    {
        UIView* subview;
        while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
            [subview removeFromSuperview];
    }

    if(tableView == tableView1)
    {
        cell.textLabel.text=[array1 objectAtIndex:indexPath.row];
    }
    else                    //Tableview2
    {
        cell.textLabel.text=[array2 objectAtIndex:indexPath.row];       
    }

    cell.textLabel.font=[UIFont fontWithName:@"Georgia" size:20];
    cell.textLabel.textAlignment=UITextAlignmentLeft;
    [cell setSelectedBackgroundView:border];    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex]; 
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark); 
    {   
        oldCell.accessoryType = UITableViewCellAccessoryNone;   
    }

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];     

    if (newCell.accessoryType == UITableViewCellAccessoryNone) 
    {   
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;  
    }   
}

Please correct me where I am wrong. Thanks in advance to all the experts.

+3
source share
4 answers

The two if statements cancel each other out, just add something else like the following:

UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex]; 
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) 
{   
    oldCell.accessoryType = UITableViewCellAccessoryNone;   
}
else
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];     

    if (newCell.accessoryType == UITableViewCellAccessoryNone) 
    {   
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;  
    }  
}
+2
source
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    for (id algoPath in [tableView indexPathsForVisibleRows])
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:algoPath];
        cell.accessoryType = UITableViewCellAccessoryNone;

        if(algoPath == indexPath)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    } 
}

, . , None, Checkmark. , .

+4

UITableView , ,

( , -), accessoryType cellForRowAtIndexPath:.

( , dequeueReusableCellWithIdentifier:, , )

+3

, !

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section!=0) {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

//didDeselect method in table view for removing previous checkmark

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section!=0)
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }

}
+1

Source: https://habr.com/ru/post/1763204/


All Articles