附件复选标记,在uitableview iphone中一次一个
嗨, 如何在uitableview中一次实现一个复选标记,然后如何在我的应用程序中保存该状态?请导游。 现在我有一个表,表行中的数据来自nsmutablearray。我的.m文件如下:附件复选标记,在uitableview iphone中一次一个,iphone,objective-c,Iphone,Objective C,嗨, 如何在uitableview中一次实现一个复选标记,然后如何在我的应用程序中保存该状态?请导游。 现在我有一个表,表行中的数据来自nsmutablearray。我的.m文件如下: #import "LocationSelection.h" @implementation LocationSelection @synthesize menuList, table; - (void)viewDidLoad { menuList=[[NSMutableArray allo
#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;
}
谢谢 在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;
}
}
在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;
}
}
使用
而不是
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
在您的代码中…使用
而不是
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
在您的代码中…在您的菜单列表中,您应该存储有两个键的NSDictionary:
- 位置
- 已访问(如果该位置应有复选标记)
NSDictionary*loc=[[NSDictionary alloc]initWithObjectsAndKeys
@“位置1”,@“位置”,
@"否,"曾到访,无,无";
设置单元格时,您将测试“已访问”键是否有值:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:@"visited"]){
// the location was visited
// setup the checkmark
}
最后,填写位置的名称,而不是:
nameLabel.text = [NSString stringWithFormat:@"%@",[rowArray objectAtIndex:0]];
放
希望这对您的菜单列表有所帮助,您应该在NSDictionary中储存两个键:
- 位置
- 已访问(如果该位置应有复选标记)
NSDictionary*loc=[[NSDictionary alloc]initWithObjectsAndKeys
@“位置1”,@“位置”,
@"否,"曾到访,无,无";
设置单元格时,您将测试“已访问”键是否有值:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:@"visited"]){
// the location was visited
// setup the checkmark
}
最后,填写位置的名称,而不是:
nameLabel.text = [NSString stringWithFormat:@"%@",[rowArray objectAtIndex:0]];
放
希望这对所有绘制的单元格都有帮助。他的问题是一个接一个的。&@macdev感谢你们两位的回复实际上是我的错,取消了对“UITableViewCellAccessoryDisclosureIndicator”的注释。但是应用程序崩溃时说(“调试器说”),oldCell超出了范围。修复此问题的帮助适用于所有绘制的单元格。他的问题是一个接一个的。&@macdev感谢你们的回复,事实上,取消注释“UITableViewCellAccessoryDisclosureIndicator”是我的错。但应用程序崩溃时说(“调试器说”),oldCell超出了范围。解决此问题的帮助可能是您尚未阅读的问题,检查设置accessoryType类型的函数名,感谢您的rep&code,但当我单击任何一行应用程序崩溃时。问题出在UITableViewCell*oldCell…..我已在调试器上检查它,指出变量oldCell超出范围。请帮助修复它,直到我试图修复它。@Maddy make nsindepath*lastinepath;这是一个ivar而不是局部变量。嘿,macdev,非常感谢!我已经正确地实现了它,现在我正在阅读关于保持勾选行值的教程!准备好我可能会问你问题:)可能是你还没有读过的问题,检查你设置accessoryType的函数名,感谢您的rep&code,但当我单击任何一行应用程序崩溃时。问题出在UITableViewCell*oldCell…..我已在调试器上检查它,说变量oldCell超出范围。请帮助修复它,直到我尝试修复它。@Maddy make NSIndexPath*lastIndexPath;这是一个ivar而不是局部变量。嘿,macdev,非常感谢!我已经正确地实现了它,现在我正在阅读关于保持勾选行值的教程!准备好我可以向u提问:)然后在表格视图:didSelectRowAtIndexPath:
中,您将获得所选的行,将所有“选中”字段设置为否,将所选行的“选中”字段的状态设置为是,然后调用[表格视图重新加载数据]
刷新显示。(并运行行取消选择动画,a-course。)这是“一次一个”部分。然后在tableView:DidSelectRowatineXpath:
中,您将获得选中的行,将所有“选中”字段设置为否,将选中行的“选中”字段的状态设置为是,然后调用[tableView重新加载数据]
刷新显示。(并运行行取消选择动画,a-course。)这是“一次一个”部分。