2016-07-04 22 views
1

更新在UITableView中先前选择的行我试图像这样...在cellForRowAtIndex()如何显示与选中标记选择的行,而在目标C

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *[email protected]"SimpleTableViewIdentifier"; 

     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier]; 

    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier]; 
    } 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row]; 

return cell; 
} 

和以下didSelectRowAtIndexPath方法的代码行中加入..

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

但是,在更新以前选择的值,查马克没有显示onpreviously选择rows..any一个可以在这个issue..Thanks帮助提前.. :)

+0

你是什么意思“更新以前的选定值”? – Arun

+0

thq..for ur response .. :) .. –

+0

以前我从表格视图中选择行...选择出现选中行上的复选标记...现在我想在出发之前在先前选择的行上显示agian复选标记更新... –

回答

2

我认为这是你需要做的。在cellForRowAtIndexPath方法,而不是对所有小区分配cell.selectionStyle = UITableViewCellSelectionStyleNone;,其分配给无仅对于未被选择

更改didSelectRowAtIndexPath方法所选择的小区的细节存储到阵列

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[self.selectedCellArray addObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

现在,这些细胞改变cellForRowAtIndexPath方法这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *[email protected]"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; 
} 
+0

我的问题是,我想跟踪lastSelectedRows带有复选标记以识别lastSelectedIndexed值,同时填充表视图数据.... –

+0

我已经用代码更新了我的答案。请通过Arun先生检查 – Arun

+0

。 :) ...但我仍然没有得到,以前选中的行与编辑模式中的复选标记... –

1

我得到了它塔米姆。检查下面的答案。我尝试了示例项目。它工作正常。

#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 
+0

以前发布的代码已经为我工作..但我应该如何取消选择以前选择的值... –

+0

我已经做了一切兄弟。检查我的代码once.You得到everything.I实现选择,取消选择并保存最后选定的行,并检查条件是否最后选择的行索引是否等于indexPath.row或不。我在编码兄弟做的一切。 – user3182143

+0

ok..Thq ...:)... –

相关问题