Create a plist
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN”“http://www.apple.com/DTDs/PropertyList-1.0.dtd“>
<plist version=“1.0”>
<array>
<dict>
<key>FirstName</key>
<string>Steve</string>
<key>LastName</key>
<string>Jobs</string>
<key>EmailId</key>
<string>stevejobs@apple.com</string>
<key>PhoneNo</key>
<string>+19876543210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Johnathan</string>
<key>LastName</key>
<string>Ive</string>
<key>EmailId</key>
<string>johnyive@apple.com</string>
<key>PhoneNo</key>
<string>+11876543210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Tim</string>
<key>LastName</key>
<string>Cook</string>
<key>EmailId</key>
<string>timcook@apple.com</string>
<key>PhoneNo</key>
<string>+12876543210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Bill</string>
<key>LastName</key>
<string>Gates</string>
<key>EmailId</key>
<string>bill@microsoft.com</string>
<key>PhoneNo</key>
<string>+13876543210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Douglas</string>
<key>LastName</key>
<string>Larsson</string>
<key>EmailId</key>
<string>douglas@microsoft.com</string>
<key>PhoneNo</key>
<string>+13874543210</string>
</dict>
<dict>
<key>FirstName</key>
<string>William</string>
<key>LastName</key>
<string>Seyf</string>
<key>EmailId</key>
<string>william@microsoft.com</string>
<key>PhoneNo</key>
<string>+138745433210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Randy</string>
<key>LastName</key>
<string>Otten</string>
<key>EmailId</key>
<string>randy@lenovo.com</string>
<key>PhoneNo</key>
<string>+13467433210</string>
</dict>
<dict>
<key>FirstName</key>
<string>Robert</string>
<key>LastName</key>
<string>Pattinsion</string>
<key>EmailId</key>
<string>robby@lenovo.com</string>
<key>PhoneNo</key>
<string>+1342356710</string>
</dict>
<dict>
<key>FirstName</key>
<string>Andy</string>
<key>LastName</key>
<string>Smith</string>
<key>EmailId</key>
<string>andysmith@lenovo.com</string>
<key>PhoneNo</key>
<string>+1342323710</string>
</dict>
<dict>
<key>FirstName</key>
<string>Fed</string>
<key>LastName</key>
<string>Peterson</string>
<key>EmailId</key>
<string>fedp@google.com</string>
<key>PhoneNo</key>
<string>+1345673710</string>
</dict>
<dict>
<key>FirstName</key>
<string>Charles</string>
<key>LastName</key>
<string>Atkinson</string>
<key>EmailId</key>
<string>rov@google.com</string>
<key>PhoneNo</key>
<string>+1312355610</string>
</dict>
</array>
</plist>
in ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>
{
BOOL isSearching;
CGFloat width,height;
}
@property (nonatomic, weak) IBOutletUITableView *tableView;
@property (nonatomic, weak) IBOutletUIScrollView *scrlContacts;
@property (nonatomic, strong) NSMutableArray *ary_contacts;
@property (nonatomic, strong) NSMutableArray *ary_filtered;
@property (nonatomic, strong) NSMutableArray *ary_selectedContacts;
@end
in ViewController.m
#import “ViewController.h”
#import “DetailViewController.h”
#define kHeightPadding 45
#define kWidthPadding 5
#define y 64
@interfaceViewController ()
@end
@implementation ViewController
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self)
{
self.ary_selectedContacts = [NSMutableArrayarray];
width = 0;
height = 0;
}
returnself;
}
– (void)viewDidLoad
{
[superviewDidLoad];
[self.searchDisplayController.searchContentsController.navigationControllersetNavigationBarHidden:YES];
NSString* plistPath = [[NSBundlemainBundle] pathForResource:@”Contacts”ofType:@”plist”];
NSArray *contentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSSortDescriptor *sort = [NSSortDescriptorsortDescriptorWithKey:@”FirstName”ascending:YES];
NSArray *aryDesc = [NSArray arrayWithObject:sort];
self.ary_contacts = [NSMutableArrayarrayWithArray:[contentArray sortedArrayUsingDescriptors:aryDesc]];
}
– (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
# pragma mark – UISearchBarDelegate
– (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
returnYES;
}
– (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
}
– (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
returnYES;
}
– (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
}
– (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
isSearching = TRUE;
NSPredicate *resultPredicate = [NSPredicatepredicateWithFormat:@”FirstName CONTAINS[cd] %@ or LastName CONTAINS[cd] %@”,searchText,searchText];
NSArray *matches = [self.ary_contacts filteredArrayUsingPredicate:resultPredicate];
if (matches.count>0)
self.ary_filtered = [NSMutableArray arrayWithArray:matches];
[self.tableViewreloadData];
}
– (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
}
– (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
isSearching = FALSE;
[self.tableViewreloadData];
}
#pragma mark – UITableViewDataSource & UITableViewDelegate
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isSearching)
return self.ary_filtered.count;
else
return self.ary_contacts.count;
}
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @”cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
NSString *text;
if (!isSearching)
text = [[[self.ary_contactsobjectAtIndex:indexPath.row] valueForKey:@”FirstName”] stringByAppendingFormat:@” %@”,[[self.ary_contactsobjectAtIndex:indexPath.row] valueForKey:@”LastName”]];
else
text = [[[self.ary_filteredobjectAtIndex:indexPath.row] valueForKey:@”FirstName”] stringByAppendingFormat:@” %@”,[[self.ary_filteredobjectAtIndex:indexPath.row] valueForKey:@”LastName”]];
cell.textLabel.text = text;
return cell;
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.scrlContacts.hidden = FALSE;
NSString *text;
if (!isSearching)
text = [[[self.ary_contactsobjectAtIndex:indexPath.row] valueForKey:@”FirstName”] stringByAppendingFormat:@” %@”,[[self.ary_contactsobjectAtIndex:indexPath.row] valueForKey:@”LastName”]];
else
text = [[[self.ary_filteredobjectAtIndex:indexPath.row] valueForKey:@”FirstName”] stringByAppendingFormat:@” %@”,[[self.ary_filteredobjectAtIndex:indexPath.row] valueForKey:@”LastName”]];
if ([self.ary_selectedContactscontainsObject:text])
{
UIAlertView *alerView = [[UIAlertViewalloc] initWithTitle:@”Information !!!”message:@”Contact has already added.”delegate:nilcancelButtonTitle:@”Ok”otherButtonTitles:nil];
[alerView show];
}
else
[UIViewanimateWithDuration:0.4delay:0options:UIViewAnimationOptionCurveEaseInanimations:^{
[self.ary_selectedContacts addObject:text];
[selfsetUpViewForContactSelection:text WithTag:indexPath.row];
} completion:^(BOOL finished) {
}];
}
– (void)setUpViewForContactSelection:(NSString *)text WithTag:(int)tag
{
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@”HelveticaNeue” size:14]};
CGSize size = [text sizeWithAttributes:attributes];
if (width >=200)
{
height = height + kHeightPadding;
width = 0;
}
if (self.scrlContacts.frame.size.height >= 200)
{
[self.scrlContacts setFrame:CGRectMake(0, y, 320, 200)];
self.scrlContacts.contentSize = CGSizeMake(320, (height>40)?(height + 50):50);
[self.tableView setFrame:CGRectMake(0, y + self.scrlContacts.frame.size.height, 320, [UIScreen mainScreen].bounds.size.height – (y + self.scrlContacts.frame.size.height))];
}
else
{
[self.scrlContacts setFrame:CGRectMake(0, y, 320, (height>40)?(height + 50):50)];
[self.tableView setFrame:CGRectMake(0, y + self.scrlContacts.frame.size.height, 320, [UIScreen mainScreen].bounds.size.height – (y + height + 50))];
}
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(width, height, size.width + 50, 35);
btn.layer.cornerRadius = 0;
btn.layer.borderColor = [UIColorblackColor].CGColor;
btn.backgroundColor = [UIColor greenColor];
btn.layer.borderWidth = 2;
btn.tag = tag;
[btn setTitle:text forState:UIControlStateNormal];
[btn setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(btnPressed:)];
longPressGesture.minimumPressDuration = 0.5;
[btn addGestureRecognizer:longPressGesture];
[self.scrlContacts addSubview:btn];
width = width + kWidthPadding + size.width + 50;
}
– (void)btnPressed:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
UIActionSheet *actionsheet = [[UIActionSheetalloc] initWithTitle:nildelegate:selfcancelButtonTitle:@”Cancel”destructiveButtonTitle:@”Delete”otherButtonTitles:@”Show Detail”, nil];
actionsheet.tag = sender.view.tag;
[actionsheet showInView:self.view];
}
}
– (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
for (UIView *subView in self.scrlContacts.subviews)
{
if ([subView isKindOfClass:[UIButton class]])
{
if(subView.tag == actionSheet.tag)
{
[subView removeFromSuperview];
[self.ary_selectedContacts removeObject:[(UIButton *)subView titleLabel].text];
}
}
}
width = 0;
height = 0;
for (UIView *subView in self.scrlContacts.subviews)
{
if ([subView isKindOfClass:[UIButton class]])
{
if (width >=200)
{
height = height + kHeightPadding;
width = 0;
}
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@”HelveticaNeue” size:14]};
CGSize size = [[(UIButton *)subView titleLabel].text sizeWithAttributes:attributes];
[self.scrlContacts setFrame:CGRectMake(0, y, 320, (height>40)?(height + 50):50)];
[self.tableView setFrame:CGRectMake(0, y + self.scrlContacts.frame.size.height, 320, [UIScreen mainScreen].bounds.size.height – (y + height + 50))];
[(UIButton *)subView setFrame:CGRectMake(width, height, size.width + 50, 35) ];
width = width + kWidthPadding + size.width + 50;
}
}
if (self.ary_selectedContacts.count == 0)
{
[self.scrlContacts setFrame:CGRectZero];
[self.tableView setFrame:CGRectMake(0, y, 320, 504)];
}
}
else if (buttonIndex == 1)
{
DetailViewController *obj_DetailViewController = [[DetailViewController alloc] initWithNibName:@”DetailViewController” bundle:nil];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@”FirstName matches %@”,[[[self.ary_selectedContactsobjectAtIndex:actionSheet.tag] componentsSeparatedByString:@” “] objectAtIndex:0]];
NSArray *array = [self.ary_contacts filteredArrayUsingPredicate:predicate];
obj_DetailViewController.dictionary = [array objectAtIndex:0];
[self.navigationController pushViewController:obj_DetailViewController animated:YES];
}
}