iOS devloper
iOS devloper

Reputation: 3

iOS:Adding checkmark to select particular row in tableview

I am doing project on contact list data so for that i will fetch the data and place it in the table view and i need to select the particular contact in a row and for that checkmark is applied after selected and checkmark is removed after deselect the contact again .

I have searched different sites but cannot able to find the exact answer please help.

Upvotes: 0

Views: 1145

Answers (2)

hybridcattt
hybridcattt

Reputation: 3041

There is a very easy solution. UITableView manages selection of rows by itself. It also supports multiple selection. Just specify how your cell will look in selected state.

Create UITableViewCell subclass as code below and use it in your UITableView. No need to override didSelectRowAtIndexPath etc.

@interface CheckCell : UITableViewCell
@end

@implementation CheckCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (self.isSelected)
       self.accessoryType = UITableViewCellAccessoryCheckmark;
    else
       self.accessoryType = UITableViewCellAccessoryNone;
}
@end

Upvotes: 0

pankaj asudani
pankaj asudani

Reputation: 852

Try this,

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

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

Upvotes: 2

Related Questions