2012-11-30 77 views
1

我是iphone開發新手。我正在開發一個應用程序,我正在顯示一些聯繫人名稱與取消選中的圖像。一旦用戶點擊按鈕選擇意味着我將它存儲在nsuserdefaults中。這裏是我嘗試過的代碼。事實上,我的問題是另一個用戶值將動態變化。我爲所有進入tableview的用戶添加一個布爾值。如果在另一個用戶中添加一個人,我想要顯示處於關閉狀態。對於其他用戶,我必須存儲nsuserdefaults中存儲的值。第一次,我手動循環0在總用戶。NSUser默認保存按鈕狀態並再次顯示

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

回答

0

正好與此代碼嘗試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; 
    } 
} 

我希望這對你有幫助..

+0

真的不知道你想說什麼 –

+0

@Rananathan你想保存數組在userdefault ..對嗎?那麼你可以用我的上面的代碼保存在userdefault globaly數組..現在明白老兄.. ?? –

+0

我可以將數組保存在nsuserdefaults中。它不是我的問題 –