如何在IOS中显示选中的已检查TableViewCell(目标c)

时间:2016-08-30 09:16:11

标签: ios objective-c

嗨朋友我是IOS的新手。我正在创建状态列表的表视图。在按钮上单击我在弹出窗口中显示TableView。创建状态列表的代码是

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
selectedState = [NSMutableArray new];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth-50, screenHeight-50)];
mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth-50, screenHeight-100)];
mytableview.delegate =self;
mytableview.dataSource=self;
[contentView addSubview:mytableview];
UIButton *save = [[UIButton alloc] initWithFrame:CGRectMake(0, screenHeight-100, screenWidth-50, 50)];
[save setTitle:@"SAVE" forState:UIControlStateNormal];
save.titleLabel.font = [UIFont systemFontOfSize:14];
[save setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[save addTarget:self action:@selector(saveActionForState:) forControlEvents:UIControlEventTouchUpInside];
[save setBackgroundColor:[UIColor colorWithRed:41.0/255.0 green:178.0/255.0 blue:165.0/255.0 alpha:1.0]];
[contentView addSubview:save];

[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];

表创建代码是

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [stateList count];

}

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
static NSString *CRTableViewCellIdentifier = @"cellIdentifier";
if(selectStateButton ==1){

        CRTableViewCell *cell = (CRTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CRTableViewCellIdentifier];

        if (cell == nil) {
            cell = [[CRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CRTableViewCellIdentifier];
        }


        NSString *text = [stateList objectAtIndex:[indexPath row]];
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.isSelected = [selectedState containsObject:text] ? YES : NO;
        cell.textLabel.text = text;




        return cell;
 }
 }

在选择表格单元格时检查了IOS的单元格。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
if(selectStateButton ==1){
    //[selectedStatesForSelect2 removeAllObjects];
NSString *text = [stateList objectAtIndex:[indexPath row]];
NSInteger indexings = [indexPath row]+1;

if ([selectedState containsObject:text]){
    [selectedState removeObject:text];
    [selectedStatesId removeObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
 }else{
     //NSLog(@"count%lu indexing %@",(unsigned long)[selectedState count],selectedStatesId);
     [selectedState addObject:text];
     [selectedStatesId addObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
     [selectedStatesForSelect2 addObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
     NSLog(@"select%@count%lu",selectedStatesForSelect2,(unsigned long)[selectedState count]);
     if([selectedState count] > 3){

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hold On..." message:@"You can't select more than three states." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [selectedState removeObject:text];
            [selectedStatesId removeObject:[NSString stringWithFormat:@"%ld", (long)indexings]];
        }

 }
}

enter image description here

就是这样看的。当用户第一次选择它时效果非常好。但是在关闭表视图后,当用户再次打开状态列表时,我想显示上一个选中的项。这不正常。我正在使用图书馆进行检查" CRMultiRowSelector"。请帮助我。

2 个答案:

答案 0 :(得分:0)

您必须保存indexPaths,在创建单元格时,您必须再次重新分配单元格状态。如果选择或不选择。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{}

在此方法中保留选定的indexPaths并在cellPathForRowIndexPath中检查相同的indexPaths,如果那里显示为已检查,否则未选中。

答案 1 :(得分:0)

我会给你所要求的。我多次尝试过这种方式。它完美无缺

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableViewCheckMarkPreviousSelectionUpdate;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
   NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark;
   NSArray *arrayFetchFromDefaults;
}

@end

@implementation ViewController

@synthesize tableViewCheckMarkPreviousSelectionUpdate;

- (void)viewDidLoad
{
   [super viewDidLoad];
   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"];
     }
   }
   [tableViewCheckMarkPreviousSelectionUpdate 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];
   }
   if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
     cell.accessoryType = UITableViewCellAccessoryNone;
   else
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
   cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
   return cell;
}

#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    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);
   }
}
@end