【问题标题】:NSUser Defaults save button status and display againNSUser Defaults 保存按钮状态并再次显示
【发布时间】:2012-11-30 05:00:23
【问题描述】:

我是 iphone 开发新手。我正在开发一个应用程序,我正在显示一些带有未选中标记图像的联系人姓名。一旦用户点击按钮选择意味着我将其存储在 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];
 }

【问题讨论】:

    标签: iphone ios uitableview nsuserdefaults


    【解决方案1】:

    只需在cellForRowAtIndexPath: 方法中使用此代码即可

    对于 CustomCell 使用如下..

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

    对于 DefaultCell 使用下面的代码...

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

    【讨论】:

    • 真的听不懂你想说什么
    • @Ranganathan 您想将数组保存在 userdefault 中。对吗?那么您可以使用我上面的代码将数组保存在 userdefault globaly 中.. 现在明白了伙计..??
    • 我可以将数组保存在 nsuserdefaults 中。这不是我的问题
    • 另请参阅此链接了解有关 NSUserDefaul mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk987654321@的更多信息
    • 0- 关闭状态。 1- 在状态下,我在数组中有三个元素,例如:xxx,yyy,zzz 我正在向新用户添加 0。如果我在 tableview 中已经有一些用户,他们最初将处于关闭状态 {0,0,0}。他们将第一次处于关闭状态。如果用户修改这三个用户的状态,它会变为 {1,0,1}。我将此值存储在 nsuserdefaults 中。我必须在表格视图中为用户显示此状态。如果再添加一个元素,我必须保持此状态,并且我还想向新用户添加 0。那么我在 nsuserdefaults 中存储的数组可能是这样的 {1,0,1,0}。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多