1

iphone開発初心者です。チェックマークを外した画像で連絡先名を表示しているアプリを開発しています。ユーザーが選択したボタンをタップすると、nsuserdefaults に保存されます。これが私が試したコードです。実際に私の問題は、別のユーザーの値が動的に変化することです。テーブルビューに来るすべてのユーザーに1つのブール値を追加しています。別のユーザーにもう1人追加された場合は、オフの状態で表示したい。他のユーザーの場合、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];
 }
4

1 に答える 1

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;
    }
}
于 2012-11-30T05:03:53.363 に答える