2016-07-04 24 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 ...:)... –

相關問題