开发者

iPhone's UITableViewCellAccessoryCheckMark

开发者 https://www.devze.com 2023-01-14 16:06 出处:网络
I have an issue in creating a CheckMarkAccessoryView in t开发者_开发问答he UITableView.When i select a row in the table i can get a checked mark for that row,but randomly some other rows in the table

I have an issue in creating a CheckMarkAccessoryView in t开发者_开发问答he UITableView.When i select a row in the table i can get a checked mark for that row,but randomly some other rows in the table also gets the check mark.I want only that cell to get the check mark.How can i do this?I am coding for an exclusive list.The following is the code which i have 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;  
    }   
}

Kindly correct me where i am going wrong. Thanks in advance for all the experts.


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

i tried different things out, nothing worked properly. now i'm iterating over all indices, setting all Accessories to None, and then the current one to accessoryCheckmark. not the cleanest approach but working.


UITableView reuses cell objects as you scroll the table, so setting the cell to show a checkmark means that it will also have a checkmark when it's reused

You need to store the state of each row in an array separate from the cells (or if you only need one cell at a time checked, just store an index somewhere), and then read from that array and set the appropriate accessoryType in the cellForRowAtIndexPath: method.

(alternatively you could simply disable cell reuse by removing the dequeueReusableCellWithIdentifier: call, but that's bad for performance if you have a long list to scroll)


The two if statement cancel each other, just add 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;  
    }  
}


I think you are looking for these methods!

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消