HI, 如何在uitableview中一次实现一个复选标记,然后如何在我的应用程序中保存该状态?请指导。 现在我有一个表,表行中的数据来自nsmutablearray.My .m文件如下:
#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;
}
谢谢!
答案 0 :(得分:5)
在cellForRowAtIndexPath中评论此行 - > cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
使用此:
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;
}
}
答案 1 :(得分:1)
在您的MenuList中,您应该存储具有2个键的NSDictionaries:
NSDictionary * loc = [[NSDictionary alloc] initWithObjectsAndKeys @“位置1”,@“位置”, @“NO”,@“visit”,nil,nil];
设置单元格时,您将测试“已访问”键是否具有值:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:@"visited"]){
// the location was visited
// setup the checkmark
}
最后,要填写位置的名称,而不是:
nameLabel.text = [NSString stringWithFormat:@"%@",[rowArray objectAtIndex:0]];
把
nameLabel.text = [[rowArray objectAtIndex:0] valueForKey:@"location"];
希望这有帮助
答案 2 :(得分:0)
使用
cell.accessoryType = UITableViewCellAccessoryCheckmark;
而不是
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
你的代码中的......