{"id":1569,"date":"2012-09-19T12:32:16","date_gmt":"2012-09-19T08:32:16","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1569"},"modified":"2013-01-07T21:15:40","modified_gmt":"2013-01-07T17:15:40","slug":"uisearchbar-and-uisearchdisplaycontroller-with-custom-cells-programmatically","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1569","title":{"rendered":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically"},"content":{"rendered":"<p>This post is not a full tutorial of how to implement this. It is just my experience description. Probably, you have the same problem with custom cells, as I had. <a href=\"http:\/\/code-ninja.org\/blog\/2012\/01\/08\/ios-quick-tip-filtering-a-uitableview-with-a-search-bar\/\" title=\"http:\/\/code-ninja.org\/blog\/2012\/01\/08\/ios-quick-tip-filtering-a-uitableview-with-a-search-bar\/\" target=\"_blank\">There are a lot of tutorials<\/a>, search them in Google. <a href=\"http:\/\/www.iphonesdkarticles.com\/2009\/01\/uitableview-searching-table-view.html\" title=\"http:\/\/www.iphonesdkarticles.com\/2009\/01\/uitableview-searching-table-view.html\" target=\"_blank\">Another nice tutorial<\/a>.<\/p>\n<p>I had highly customized cells, but couldn&#8217;t understand, why do they mess up, when search starts and self.searchDisplayController.searchResultsTableView is displayed with regular cell, but not customized cells. I tried to find solution for about 2-3 hours, then I decided to make exactly the same, as others did for using custom cells. I solved the problem, that was in the absence of two delegate properties in my code. This is the correct version, of how you should initialize your UISearchBar and UISearchDisplayController (if you are doing it programmatically). <\/p>\n<p>Code for ViewDidLoad method: <\/p>\n<pre>\r\n\/\/ Search bar\r\nsearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];\r\nsearchBar.delegate = self;\r\nsearchBar.tintColor = cell_end_color;\r\nself.tableView.tableHeaderView = searchBar;\r\n    \r\nsearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];\r\nsearchDisplayController.delegate = self;\r\nsearchDisplayController.searchResultsDataSource = self;\r\nsearchDisplayController.searchResultsDelegate = self;\r\n<\/pre>\n<p>The other parts are pretty analogous to the usual search bar usage with regular cells. For instance, look at <a href=\"http:\/\/developer.apple.com\/library\/ios\/#samplecode\/TableSearch\/Introduction\/Intro.html\" title=\"http:\/\/developer.apple.com\/library\/ios\/#samplecode\/TableSearch\/Introduction\/Intro.html\" target=\"_blank\">Apple&#8217;s source code<\/a> on this topic. <\/p>\n<p>What is interesting, this works fine on iPad too even thought the frame is 320 px here. <\/p>\n<p>References:<br \/>\n<a href=\"http:\/\/developer.apple.com\/library\/ios\/#documentation\/uikit\/reference\/UISearchBar_Class\/Reference\/Reference.html\" title=\"http:\/\/developer.apple.com\/library\/ios\/#documentation\/uikit\/reference\/UISearchBar_Class\/Reference\/Reference.html\" target=\"_blank\">UISearchBar Class reference<\/a><\/p>\n<p><a href=\"https:\/\/developer.apple.com\/library\/ios\/#documentation\/uikit\/reference\/UISearchDisplayController_Class\/Reference\/Reference.html\" title=\"https:\/\/developer.apple.com\/library\/ios\/#documentation\/uikit\/reference\/UISearchDisplayController_Class\/Reference\/Reference.html\" target=\"_blank\">UISearchDisplayController Class Reference<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is not a full tutorial of how to implement this. It is just my experience description. Probably, you have the same problem with custom cells, as I had. There are a lot of tutorials, search them in Google. Another nice tutorial. I had highly customized cells, but couldn&#8217;t understand, why do they mess [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=1569\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"This post is not a full tutorial of how to implement this. It is just my experience description. Probably, you have the same problem with custom cells, as I had. There are a lot of tutorials, search them in Google. Another nice tutorial. I had highly customized cells, but couldn&#8217;t understand, why do they mess [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1569\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-19T08:32:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-07T17:15:40+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"UISearchBar and UISearchDisplayController with Custom Cells Programmatically\",\"datePublished\":\"2012-09-19T08:32:16+00:00\",\"dateModified\":\"2013-01-07T17:15:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569\"},\"wordCount\":193,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569\",\"name\":\"UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2012-09-19T08:32:16+00:00\",\"dateModified\":\"2013-01-07T17:15:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1569#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UISearchBar and UISearchDisplayController with Custom Cells Programmatically\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=1569","og_locale":"en_US","og_type":"article","og_title":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev","og_description":"This post is not a full tutorial of how to implement this. It is just my experience description. Probably, you have the same problem with custom cells, as I had. There are a lot of tutorials, search them in Google. Another nice tutorial. I had highly customized cells, but couldn&#8217;t understand, why do they mess [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1569","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2012-09-19T08:32:16+00:00","article_modified_time":"2013-01-07T17:15:40+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1569#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1569"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically","datePublished":"2012-09-19T08:32:16+00:00","dateModified":"2013-01-07T17:15:40+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1569"},"wordCount":193,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1569#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1569","url":"https:\/\/blog.alwawee.ru\/?p=1569","name":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2012-09-19T08:32:16+00:00","dateModified":"2013-01-07T17:15:40+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1569#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1569"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1569#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"UISearchBar and UISearchDisplayController with Custom Cells Programmatically"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1569"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}