Ios 如何在更新objective c中UITableView中以前选定的行时显示带有复选标记的选定行
我试过这样…在CellforRowatinex()中 并在DidSelectRowatineXpath中添加以下代码行Ios 如何在更新objective c中UITableView中以前选定的行时显示带有复选标记的选定行,ios,objective-c,iphone,Ios,Objective C,Iphone,我试过这样…在CellforRowatinex()中 并在DidSelectRowatineXpath中添加以下代码行 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; [tableView deselectRowAtIndexPath:indexPath animated:YES]; 但是,在
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
但是,在更新以前选择的值时,复选标记不会显示在以前选择的行上。。任何人都可以在此问题上提供帮助。。提前感谢。) 我想这就是你需要做的。在
cellforrowatinexpath
方法中,而不是指定cell.selectionStyle=UITableViewCellSelectionStyleNone代码>对于所有单元格,仅对于未选择的单元格将其指定为“无”
更改didSelectRowAtIndexPath
方法以将所选单元格详细信息存储到数组中
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedCellArray addObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
现在像这样更改cellforrowatinexpath
方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableViewIdentifier=@"SimpleTableViewIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier];
}
cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row];
if (![self.selectedCellArray containsObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
我知道了,塔米姆。检查下面的答案。我尝试了示例项目。它工作正常
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark;
NSArray *arrayFetchFromDefaults;
NSInteger lastSelectedIndex;
}
@end
@implementation ViewController
@synthesize tableViewCheckMarkSelectionUpdate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"];
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults];
if(arrProductSelectDeSelectCheckMark.count == 0)
{
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
for(int j=0;j<[arrProductSelection count];j++)
{
[arrProductSelectDeSelectCheckMark addObject:@"deselected"];
}
}
[tableViewCheckMarkSelectionUpdate reloadData];
}
#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrProductSelection.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
// lastSelectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedRow"]; - Getting Last selected index row
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// if (indexPath.row == lastSelectedIndex)
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedRow"]; //This is for Last Selected IndexPath Row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
@try
{
CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate];
NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint];
NSLog(@"%@",arrProductSelectDeSelectCheckMark);
if([arrProductSelectDeSelectCheckMark count]==0)
{
for(int i=0; i<[arrProductSelection count]; i++)
{
[arrProductSelectDeSelectCheckMark addObject:@"deselected"];
}
}
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"];
[defaults synchronize];
}
@catch (NSException *exception) {
NSLog(@"The exception is-%@",exception);
}
}
- (IBAction)actionGoPrevious:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
#导入“ViewController.h”
@界面视图控制器()
{
NSMutableArray*arrProductSelection,*arrProductSelectDecelectCheckmark;
NSArray*阵列默认设置;
NSInteger最后选择的索引;
}
@结束
@实现视图控制器
@综合tableViewCheckMarkSelectionUpdate;
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
arrProductSelection=[[NSMutableArray alloc]initWithObjects:@“iPhone”@“iPad”@“iPod”@“iTV”@“iWatch”@“iMac”,nil];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)视图将显示:(BOOL)动画
{
[NSUserDefaults*userDefaults=[NSUserDefaults standardUserDefaults];
arrayFetchFromDefaults=[userDefaults objectForKey:@“selectedcheckmark”];
ArrProductSelectDeceCheckMark=[[NSMutableArray alloc]initWithArray:ArrayFetchFromDefault];
如果(arrProductSelectDecelectCheckMark.count==0)
{
ArrProductSelectDecleCheckmark=[[NSMutableArray alloc]init];
对于(int j=0;j“更新先前选择的值”是什么意思?对于u r响应:)…以前我从表视图中选择了行…在选择“在选定行上显示复选标记”时…现在我想在进行更新之前在以前选择的行上显示一个复选标记…您是否刷新了中间的表视图?您的意思是重新加载表视图?…我关心的是,我想用复选标记跟踪lastSelectedRows用于在填充表视图数据时识别lastSelectedIndexed值…thq先生:…..但我仍然没有得到以前在编辑模式下带有复选标记的选定行…抱歉,我错过了一个位代码。请现在用新的更新检查我的答案您所做的错误是重新加载表视图,这将重置选择所有单元格的le返回到none。我们现在做的是保存已选中单元格的状态,并将其更新为cellForRowAtIndexPath方法中的selected。以前发布的代码对我有效。但是我应该如何取消选择以前选择的值…我已经完成了所有操作。兄弟,检查我的代码一次。你得到了所有内容。我实现了Select,取消选择并保存最后选择的行,并检查条件最后选择的行索引是否等于indexath.row。在我的编码兄弟中所做的一切。检查并告诉我。比较两个答案。勾选最佳有用的答案。它应该满足您的所有要求。选择,取消选择,选择索引行,以便我们回来时显示。E一切都应该正常。hi varun..GM..yester代码很好..但它只显示最后一个索引值…但我想用复选标记显示多个值。。
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark;
NSArray *arrayFetchFromDefaults;
NSInteger lastSelectedIndex;
}
@end
@implementation ViewController
@synthesize tableViewCheckMarkSelectionUpdate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"];
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults];
if(arrProductSelectDeSelectCheckMark.count == 0)
{
arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
for(int j=0;j<[arrProductSelection count];j++)
{
[arrProductSelectDeSelectCheckMark addObject:@"deselected"];
}
}
[tableViewCheckMarkSelectionUpdate reloadData];
}
#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrProductSelection.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
// lastSelectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedRow"]; - Getting Last selected index row
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// if (indexPath.row == lastSelectedIndex)
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedRow"]; //This is for Last Selected IndexPath Row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
@try
{
CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate];
NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint];
NSLog(@"%@",arrProductSelectDeSelectCheckMark);
if([arrProductSelectDeSelectCheckMark count]==0)
{
for(int i=0; i<[arrProductSelection count]; i++)
{
[arrProductSelectDeSelectCheckMark addObject:@"deselected"];
}
}
if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"];
[defaults synchronize];
}
@catch (NSException *exception) {
NSLog(@"The exception is-%@",exception);
}
}
- (IBAction)actionGoPrevious:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end