Reputation: 4111
HI, how to implement checkmark, one at a time in a uitableview.and then how to save that state in my application? please guide. right now i am having a table and the data in table row is coming from a nsmutablearray.My .m file is as:
#import "LocationSelection.h"
@implementation LocationSelection
@synthesize menuList, table;
- (void)viewDidLoad {
menuList=[[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:@"LOCATION1",nil],
[NSArray arrayWithObjects:@"LOCATION2",nil],
[NSArray arrayWithObjects:@"LOCATION3",nil],
[NSArray arrayWithObjects:@"LOCATION4",nil],
[NSArray arrayWithObjects:@"LOCATION5",nil],
[NSArray arrayWithObjects:@"LOCATION6",nil],
[NSArray arrayWithObjects:@"LOCATION7",nil],
nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
self.title=@"Location Selection";
[table reloadData];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{
return menuList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted=NO;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSArray *rowArray = [menuList objectAtIndex:indexPath.row];
UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)]autorelease];
nameLabel.text = [NSString stringWithFormat:@"%@",[rowArray objectAtIndex:0]];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.shadowColor=[UIColor whiteColor];
nameLabel.shadowOffset=CGSizeMake(0.0, 0.5);
nameLabel.textColor = RGB(0,0,0);
[nameLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[cell.contentView addSubview:nameLabel];
return cell;
}
Thanks!
Upvotes: 0
Views: 4284
Reputation: 6401
Comment this line in cellForRowAtIndexPath -> cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Use this:
NSIndexPath* lastIndexPath; // This as an ivar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
}
Upvotes: 5
Reputation: 2103
In your MenuList you should stock NSDictionaries having 2 keys:
NSDictionary *loc = [[NSDictionary alloc] initWithObjectsAndKeys @"Location 1", @"location", @"NO", @"visited", nil, nil];
When setting up the cell you would test to see if the "visited" key has a value:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:@"visited"]){
// the location was visited
// setup the checkmark
}
Lastly, to fill in the name of the location, instead of:
nameLabel.text = [NSString stringWithFormat:@"%@",[rowArray objectAtIndex:0]];
put
nameLabel.text = [[rowArray objectAtIndex:0] valueForKey:@"location"];
Hope this helps
Upvotes: 1
Reputation: 31730
Use
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Instead of
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
in your code ...
Upvotes: 0