2016-08-30 2 views
0

こんにちは私はIOSで新しいです。私は状態リストのテーブルビューを作成しています。ボタンをクリックすると、ポップアップでTableViewが表示されます。状態リストを作成するためのコードは、テーブル作成コードの場合IOSで選択されたチェックTableViewCellを表示する方法(目的c)

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]; 

は、テーブルセルの選択にIOSのセルを確認

- (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; 
} 
} 

です。このように見て

- (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"のためのライブラリを使用しています。私を助けてください。

+0

ウダイ・クマール私のcoding.Itを試してみてくださいを何度も試みた完璧

作品this.It何与えます完璧に動作します。 – user3182143

答えて

0

私はあなたask.Iが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 
0

インデックスパスを保存する必要があります。また、セルを作成する際には、セルの状態を再割り当てする必要があります。選択されているかどうか。それ以外の場合は未チェックチェックするとそこに表示されている場合、この方法では

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

、選択indexPathsを維持し、cellPathForRowIndexPathに同じindexPathsを確認してください。

+0

あなたのリプレイをありがとう。私はこのことを行い、正解を示していますが、テーブルビューをスクロールするとレイアウトが歪んでしまいます。前の選択を解除した後、再び選択しません。おそらくデキューされたセルを取得した後に、再度データを設定する必要があります。 –

+0

あなたがスクロールするときに、tableViewのデキューをすると、このセルがプレーン・コンテンツ・ビュー・セルになります。何もそこにはありません、あなたはすべてを再び描く必要があります。 – Sandy

関連する問題