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