NSUserDefaults保存按钮状态并再次显示

时间:2012-11-30 05:00:23

标签: iphone ios uitableview nsuserdefaults

我是iphone开发的新手。我正在开发一个应用程序,因为我正在使用uncheckmark图像显示一些联系人姓名。一旦用户点击按钮到选定的意味着我将它存储在nsuserdefaults中。这是我试过的代码。实际上我的问题是另一个用户值将动态改变。我为所有进入tableview的用户添加一个布尔值。如果在另一个用户中添加了一个人,我希望在关闭状态下显示。对于其他用户,我必须存储存储在nsuserdefaults中的值。我第一次在总用户中手动循环0。

My interface File
#import <UIKit/UIKit.h>

 @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>    
NSArray * test;
IBOutlet UITableViewCell *customCell;
IBOutlet UIImageView *photo;
IBOutlet UILabel *userName;
NSMutableArray *dataArray;
NSMutableDictionary *tempDictionary;
NSMutableArray * selected;
NSUserDefaults *prefs;
NSMutableArray * statusArray;
NSMutableArray * prefsArray;

}
 @property(nonatomic,retain)    IBOutlet UITableViewCell *customCell;
 @property(nonatomic,retain)IBOutlet UITableView *tblView;
  @property(nonatomic,retain) NSMutableArray *dataArray;
 @end


implementation file:
- (void)viewDidLoad
{
dataArray=[[NSMutableArray alloc]init];
selected=[[NSMutableArray alloc]init];
prefs=[NSUserDefaults standardUserDefaults];
prefsArray=[[NSMutableArray alloc]init];


NSMutableArray * anotheruser=[NSMutableArray arrayWithObjects:@"da",@"kn",@"gn",@"Prd",@"Kai",@"Sh",nil];
for (int i=0; i<anotheruser.count;i++) {
    [selected addObject:[NSNumber numberWithInt:0]];
}
for (int i=0; i< anotheruser.count; i++) {
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict setValue:[anotheruser objectAtIndex:i] forKey:@"name"];
    [dict setValue:[selected objectAtIndex:i] forKey:@"checked"];
    [self.dataArray addObject:dict];
}

[super viewDidLoad];
 }
    (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:NSInteger)section
  {
   return dataArray.count;
   }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
 {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"MyViewCell" owner:self options:nil];
    cell = customCell;
    self.customCell = nil;

}

photo.image=[UIImage imageNamed:@"raj.jpg"];
NSMutableDictionary *item = [self.dataArray objectAtIndex:indexPath.row];
userName.text= [item objectForKey:@"name"];

[item setObject:cell forKey:@"cell"];

BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;   // match the button's size with the image size

[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

return cell;
}
   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
[self tableView: tblView accessoryButtonTappedForRowWithIndexPath: indexPath];
[self.tblView deselectRowAtIndexPath:indexPath animated:YES];
}

    - (void)checkButtonTapped:(id)sender event:(id)event
  {
  NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView:tblView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
 }


   - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:  (NSIndexPath *)indexPath
   {
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];

NSUInteger myInt=indexPath.row;
    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];

UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;


if (!checked)
{
    [selected replaceObjectAtIndex:myInt withObject:[NSNumber numberWithInt:1]];
}else{
    [selected replaceObjectAtIndex:myInt withObject:[NSNumber numberWithInt:0]];
}

 [prefs setObject:selected forKey:@"status"];
   NSLog(@"%@", selected);
   UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage  imageNamed:@"checked.png"];
 [button setBackgroundImage:newImage forState:UIControlStateNormal];
 }

1 个答案:

答案 0 :(得分:0)

只需在cellForRowAtIndexPath:方法

中尝试使用此代码即可

对于自定义单元格的使用,如下面的..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyViewCell *cell = (MyViewCell *) [tableView dequeueReusableCellWithIdentifier:nil];

    // MyCustomeCell *cell = (MyCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyViewCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (MyViewCell *) currentObject;
                break;
            }

        }
}

和默认单元格使用以下代码...

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *kCustomCellID = @"MyCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    }
}

我希望这对你有所帮助..