Tuesday, 12 February 2013

ID Profile Source Code- DetailView Controller


//.h



#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController{
    
    IBOutlet UITableView *myTable;
    NSMutableArray *categoryArray;
    
    NSMutableArray *imageArray;
    NSMutableArray *nameArray;
    NSMutableArray *distrctArray;
    NSMutableArray *emailArray;
    NSMutableArray *mobileArray;
    
    
        
    NSUserDefaults *currentDefaults;
    
    NSData *ImageData1;
    
    int q;
}


@end

//.m

#import "DetailViewController.h"
#import "TableViewCell.h"



@implementation DetailViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    currentDefaults = [NSUserDefaults standardUserDefaults];
    
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedName"];
    NSMutableArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    nameArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
    NSLog(@"name-- %d",[nameArray count]);
    
    NSData *dataRepresentingSavedArray0 = [currentDefaults objectForKey:@"savedImage"];
    NSMutableArray *oldSavedArray0 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray0];
    imageArray = [[NSMutableArray alloc] initWithArray:oldSavedArray0];
    NSLog(@"image-- %d",[imageArray count]);
    
    NSData *dataRepresentingSavedArray1 = [currentDefaults objectForKey:@"savedDt"];
    NSMutableArray *oldSavedArray1 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray1];
    distrctArray = [[NSMutableArray alloc] initWithArray:oldSavedArray1];
    NSLog(@"distrct-- %d",[distrctArray count]);
    
    NSData *dataRepresentingSavedArray2 = [currentDefaults objectForKey:@"savedEmail"];
    NSMutableArray *oldSavedArray2 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray2];
    emailArray = [[NSMutableArray alloc] initWithArray:oldSavedArray2];
    NSLog(@"email-- %d",[emailArray count]);
    
    NSData *dataRepresentingSavedArray3 = [currentDefaults objectForKey:@"savedMobile"];
    NSMutableArray *oldSavedArray3 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray3];
    mobileArray = [[NSMutableArray alloc] initWithArray:oldSavedArray3];
    NSLog(@"mobile-- %d",[mobileArray count]);
    
  
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //   [self.navigationController.navigationBar setAlpha:1.0];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [nameArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     
    static NSString *cellID= @"TableViewCell";
    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    
    if(cell==nil)
    {
        
        
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:nil options:nil];
        
        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass: [TableViewCell class]])
            {
                cell = (TableViewCell *)currentObject;
            }
            
        }
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 50, 49, 19);
    //[button setBackgroundColor:[UIColor clearColor]];
    UIImage *buttonImage = [UIImage imageNamed:@"DoneN.png"];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(del:)forControlEvents:UIControlEventTouchUpInside];
    button.tag = indexPath.row;
    [cell.contentView addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(250, 15, 50, 20);
    [button1 setBackgroundColor:[UIColor clearColor]];
    
    UIImage *buttonImage1 = [UIImage imageNamed:@"edit.png"];
    [button1 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    
    
    [button1 addTarget:self action:@selector(edit:)forControlEvents:UIControlEventTouchUpInside];
    button1.tag = indexPath.row;
    [cell.contentView addSubview:button1];
    
    // Configure the cell.
    cell.title1.text=[nameArray objectAtIndex:indexPath.row];
    cell.subTitle1.text=[distrctArray objectAtIndex:indexPath.row];
    NSData *imgData = (NSData*)[imageArray objectAtIndex:indexPath.row];
    UIImage* imge = [[UIImage alloc] initWithData:imgData];
    cell.img1.image=imge;
        
    return cell;
}
-(void)del:(id)sender
{

    UIButton *tappedButton = (UIButton*)sender;
    q=tappedButton.tag;
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Are you sure to delete this Profile?" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Don't Allow", nil];
    [alert show];
  
    
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
if (buttonIndex == 0) {
      
        NSLog(@"Tag--%d",q);
        
        [nameArray removeObjectAtIndex:q];
        [imageArray removeObjectAtIndex:q];
        [distrctArray removeObjectAtIndex:q];
        [emailArray removeObjectAtIndex:q];
        [mobileArray removeObjectAtIndex:q];
    
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:imageArray] forKey:@"savedImage"];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:nameArray] forKey:@"savedName"];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:distrctArray] forKey:@"savedDt"];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:emailArray] forKey:@"savedEmail"];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:mobileArray] forKey:@"savedMobile"];

        [[NSUserDefaults standardUserDefaults] synchronize];
    
        [myTable reloadData];
        [self.view setNeedsDisplay];
    }
if (buttonIndex == 1) {
        NSLog(@"button");
    }
    
    
}
-(void)edit:(id)sender{
    
    UIButton *tappedButton = (UIButton*)sender;
    q=tappedButton.tag;
    NSString *index=[NSString stringWithFormat:@"%d",q];
    [currentDefaults setObject:index forKey:@"edit"];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

No comments:

Post a Comment