Sharing the UISearchController search bar UI Technology in iOS

Source: Internet
Author: User

Sharing the UISearchController search bar UI Technology in iOS

 

UISearchController inherits from UIViewController

// In iOS 8.0 and later versions, you can use UISearchController to conveniently add search boxes to UITableView. in earlier versions, you must use the combination of UISearchBar and UISearchDisplayController.

# Import SearchViewController. h # define kTableCell @ tablecell @ interface SearchViewController ()
 
  
@ Property (nonatomic, retain) NSMutableArray * items; @ property (nonatomic, retain) UISearchController * searchController; @ property (nonatomic, retain) NSMutableArray * searchResults; // receive data source results @ end
 

Lazy loading and release

 

@ Implementation SearchViewController-(instancetype) init {self = [super init]; if (self) {} return self;} // lazy loading-(NSMutableArray *) items {if (! _ Items) {self. items = [NSMutableArray arrayWithCapacity: 0];} return [_ items retain] autorelease];} // lazy loading-(NSMutableArray *) searchResults {if (! _ SearchResults) {self. searchResults = [NSMutableArray arrayWithCapacity: 0];} return [_ searchResults retain] autorelease];} // release-(void) dealloc {self. items = nil; self. searchResults = nil; self. searchController = nil; [super dealloc];}

Call:

 

 

-(Void) viewDidLoad {[super viewDidLoad]; // navigation bar [self p_navigationController]; // call to initialize searchController [self setSearchControllerView]; // register [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: kTableCell];}

 

Initialize SearchController

 

-(Void) setSearchControllerView {self. searchController = [[UISearchController alloc] initWithSearchResultsController: nil]; self. searchController. searchBar. frame = CGRectMake (0, 0, 0, 44); self. searchController. dimsBackgroundDuringPresentation = false; // the self. tableView. tableHeaderView = self. searchController. searchBar; [self. searchController. searchBar sizeToFit]; // background color self. searchController. searchBar. backgroundColor = [UIColor orangeColor]; self. searchController. searchResultsUpdater = self ;}

 

// After UISearchController is introduced, the content of UITableView must be changed accordingly: whether the content to be presented in the cell is items or searchResults. you can use the active attribute of UISearchController to determine whether the input box is in the active state.

// Many UITableView-related methods must be judged based on the active state:


Configure navigation bar

 

-(Void) p_navigationController {self. title = @ Xiaohan-hero list; self. items = @ [@ No.1 Stinky Tofu CN ., @ Bill Ji walter, @ Wallonia Valoran, @ Zaun, @ demassia Demacia, @ Noxus, @ Ionia, @ pitvov Piltover, @ freelzhoud Freijord, @ Bandle City, @ fearless pioneer, @ The Institute of War, @ jushenfeng, @ JustThunder, @ JustRule ), @ Black Rose, @ Shadow island, @ Steel fierce, @ Shurima Desert, @ Balanced sect ), @ Crystal Scar, @ Shadow Flow, @ The Watchtower of sea, @ maid, @ conquer The sea, @ Twisted jungle Twisted Treeline, @ Education Network Area, @ trial site Proving Grounds, @ Kalamanda, @ Blue Flame Island on Blue Flame Island, @ wail swamp Howling Marsh, @ Icathia, @ Ironspike Mountains, @ Kumungu, @ Lokfar, @ Morgon Pass, @ targoshan Mountain Targon, @ Plague Jungles, @ Serpentine River, @ Urtistan, @ Voodoo Lands, @ roaring Abyss Howling Abyss, @ Magma Chambers, @ Summoner Canyon Summoner's Rift, @ nine-tailed Demon Fox: Ahri, @ shadow Fist: Akali, @ tauru leader: Alistar ), @ mummy: Amumu, @ ice crystal Phoenix: Anivia, @ dark girl: Annie, @ ice cold SHOOTER: Ashe ), @ steam ROBOT: Blitzcrank, @ revenge flame SOUL: Brand, @ Picheng female: Caitlyn, @ magic snake: cassiopeia, @ void fear: ChoGath, @ hero: Corki, @ noshus: Darius ), @ goddess of the moon: Diana :( Diana), @ zu'an mad man: DrMundo, @ Glory: Delevin, @ queen of the spider: Elise ), @ Widow Maker: Evelynn, @ Explorer: Ezreal, @ doomsday MESSENGER: Fiddlesticks, @ wushuang Jian Ji: Fiora ), @ tidal hailing: Fizz, @ The Sentinel: Galio, @ the disaster of the Ocean: plank (Gangplank), @ the power of demassia: Garen ), @ Bucket: Gragas, @ out-of-law Madman: Graves, @ war: Hecarim, @ Dafa mingjia: heimeringer, @ blade will: Irelia, @ storm anger: Janna, @ de masia Emperor: Jarvan IV ), @ weapon master: Jax, @ future guardian: Jayce, @ Tianqi: Karma, @ song of death: Karthus ), @ void Walker: Kassadin, @ unknown Blade: Katarina, @ judge ANGEL: Kayle, @ Violent Heart: Kenan ), @ void: Khazix (Khazix), @ abyss giant mouth: Kog Maw (Kog Maw: lee sin, @ goddess sugon: Leona, @ fairy Witch: lulu, @ glorious GIRL: Lux, @ lava BEAST: malphite, @ void precognition: Malzahar, @ twist tree: Maokai, @ promise Swordsman: Yi, @ bounty hunter: missFortune, @ Qi Tiansheng: Monkey king, @ metal master: Mordekaiser, @ Fallen Angel: Morgana, @ Chao Xiaoji: nami, @ Desert Death: Nasus, @ Deep-Sea titanes: Nautilus, @ wild female HUNTER: Nidalee, @ permanent nightmare: magenton (Nocturne), @ snowman Knight: Nunu, @ Mad Warrior: Olaf, @ clockwork: Orianna, @ king of war: pantheon, @ ambassador to iron and steel: Bobby (Poopy), @ armored TURTLE: Rammus, @ desert BUTCHER: Renekton, @ aozhi chaser: rengar, @ exile Blade: Rivan, @ mechanical Public Enemy: Rumble, @ wandering Mage: Ryze, @ winter anger: sejuani :( Sejuani), @ demon clown: SACO (Shaco), @ muguang eye: Shen (Shen), @ Longxue Wuji: xvara (Shyvana), @ Alchemy: singed, @ undead warrior: Sion, @ goddess of war: Sivir, @ crystal pioneer: Skarner, @ lyse FAIRY: sona, @ son of all stars: Soraka, @ cetleader: Swain, @ dark head: Syndra ), @ shadow of Blade: Talon, @ diamond Knight: Taric, @: Teemo, @ Thresh ), @ milin GUNNER: Tristana, @ curse: Trundle, @ the king of the barbarian: Tryndamere, @ card master: twisted Fate, @ source of plague: Twitch, @ spirit of the Beast: Udyr, @ pride of LEADER: Urgot ), @ disciplinary Arrow: verus (Varus), @ dark night hunter: Vayne, @ evil little Mage: Veigar, @ Picheng executive judge: Wei (Vi ), @ mechanical pioneer: Victor (Viktor), @ scarlet receiver: Vladimir (Vladimir), @ thunder roar: Volibear, @ bloodthirsty HUNTER: Warwick ), @ ancient Wuling: zeras (Xerath), @ Debon MANAGER: XinZhao (XinZhao), @: Yorick (Yorick), @ shadow owner: Zed ), @ blow up the ghost talents: gigs (Ziggs), @ time guardian: Zilean, @ thorns: Zyra]. mutableCopy ;}

 

# Methods in The pragma mark Protocol

-(Void) updateSearchResultsForSearchController :( UISearchController *) searchController {[self. searchResults removeAllObjects]; // NSPredicate preprespredicate * searchPredicate = [NSPredicate predicateWithFormat: @ self contains [cd] % @, searchController. searchBar. text]; self. searchResults = [[self. items filteredArrayUsingPredicate: searchPredicate] mutableCopy]; // refresh the table [self. tableView reloadData];}

# Pragma mark-Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {        // Return the number of sections.    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {        return (!self.searchController.active) ? self.items.count : self.searchResults.count;}

Return cell:

 

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableCell forIndexPath:indexPath];        if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kTableCell];    }            if (indexPath.row > 0) {        cell.imageView.frame = CGRectMake(60, 60, 10, 10);        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@%ld,indexPath.row + 43]];        cell.textLabel.font = [UIFont systemFontOfSize:15];    }    cell.textLabel.text = (!self.searchController.active) ? self.items[indexPath.row] : self.searchResults[indexPath.row];        return cell;}

Business proxy return height:

 

 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == 0) {        return 100  ;    }else{        return 80;    }}

Final effect:

 




 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.