{"id":1795,"date":"2013-09-24T13:39:13","date_gmt":"2013-09-24T09:39:13","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1795"},"modified":"2013-09-24T20:20:13","modified_gmt":"2013-09-24T16:20:13","slug":"instructions-on-how-to-create-custom-uitableviewcell-with-a-xib","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1795","title":{"rendered":"Instructions on how to create custom UITableViewCell with a xib"},"content":{"rendered":"<p>It is a very fast and convenient way to create a custom <strong>UITableViewCell<\/strong> layout using <strong>Interface Builder<\/strong>. But <strong>Apple<\/strong> for some reason has not made this easy &#8211; you have to know some magic things to perform this task. There is a a ton of tutorials on this topic already. But nevertheless I decided to write my own one. So, these are step by step instructions: <\/p>\n<p>1. Create a <strong>UITableViewCell<\/strong> subclass, let&#8217;s name it <strong>RRMessageCell<\/strong>. When you create it, you will not get a <strong>xib<\/strong> by default, because <strong>XCode<\/strong> doesn&#8217;t create it.<br \/>\n2. Add a <strong>xib<\/strong> file called <strong>RRMessageCell<\/strong>.<br \/>\n3. In your <strong>xib<\/strong> file delete a view by default and add <strong>UITableViewCell<\/strong> object.<br \/>\n4. Set a class for that new cell in <strong>xib<\/strong> and for <strong>File&#8217;s owner<\/strong> <strong>RRMessageCell<\/strong> in a right panel, 3rd tab of the <strong>Interface Builder<\/strong>.<br \/>\n5. Add elements to your cell in xib: labels, imageViews, etc.<br \/>\n6. Create outlets in your <strong>RRMessageCell<\/strong> class.<br \/>\n7. Here is a first magic: do not bind outlets via files&#8217; owner, but bind them via your <strong>RRMessageCell<\/strong>. It should look like this when you right click of your file&#8217;s owner:<br \/>\n<a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.30.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.30-300x131.png\" alt=\"\" title=\"\u0421\u043d\u0438\u043c\u043e\u043a \u044d\u043a\u0440\u0430\u043d\u0430 2013-09-24 \u0432 13.27.30\" width=\"300\" height=\"131\" class=\"aligncenter size-medium wp-image-1796\" srcset=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.30-300x131.png 300w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.30.png 844w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nAnd it should look like this when you right click of a cell view:<br \/>\n<a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.13.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.13-300x124.png\" alt=\"\" title=\"\u0421\u043d\u0438\u043c\u043e\u043a \u044d\u043a\u0440\u0430\u043d\u0430 2013-09-24 \u0432 13.27.13\" width=\"300\" height=\"124\" class=\"aligncenter size-medium wp-image-1797\" srcset=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.13-300x124.png 300w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.27.13.png 841w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n8. Now second magic: select your cell view in a xib file and in a 4th tab of right panel (attributes) set Identifier to &#8216;<strong>RRMessageCell<\/strong>&#8216;. Here is illustration:<br \/>\n<a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.42.55.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.42.55-300x102.png\" alt=\"\" title=\"\u0421\u043d\u0438\u043c\u043e\u043a \u044d\u043a\u0440\u0430\u043d\u0430 2013-09-24 \u0432 13.42.55\" width=\"300\" height=\"102\" class=\"aligncenter size-medium wp-image-1801\" srcset=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.42.55-300x102.png 300w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2013\/09\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2013-09-24-\u0432-13.42.55.png 469w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n9. Now you can close a <strong>xib<\/strong> file, open implementation file and write some code to set contents of a cell, using a custom <strong>setter<\/strong> for object, that is displayed in your cell class <strong>RRMessageCell.m<\/strong>: <\/p>\n<pre>\r\n- (void)setMessage:(RRMessage *)message\r\n{\r\n  _message = message;\r\n  \r\n  self.topicLabel.text = message.messageTheme;\r\n  self.bodyLabel.text = message.messageText;\r\n  \r\n  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\r\n  [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];\r\n  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];\r\n  NSString *dateString = [dateFormatter stringFromDate:message.messageDate];\r\n  self.dateLabel.text = [NSString stringWithFormat:@\"\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e: %@\", dateString];\r\n  \r\n  self.arrowImageView.image = ([message.messageFromEmployer intValue] == 0) ? [UIImage imageNamed:@\"arrow_from\"] : [UIImage imageNamed:@\"arrow_to\"];\r\n}\r\n<\/pre>\n<p>10. And now third magic &#8211; you have to create a cell this way in your <strong>UITableViewController<\/strong>:<\/p>\n<pre>\r\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\r\n{\r\n  \r\n  static NSString *CellIdentifier = @\"RRMessageCell\";\r\n  \r\n  RRMessageCell *cell = (RRMessageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];\r\n  \r\n  if ( cell == nil ) {\r\n    \r\n    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@\"RRMessageCell\" owner:self options:nil];\r\n    cell = topLevelObjects[0];\r\n    \r\n  }\r\n  \r\n  RRMessage *message = _messages[indexPath.row];\r\n  \r\n  [cell setMessage:message];\r\n  \r\n  return cell;\r\n}\r\n<\/pre>\n<p>P.S. <strong>Small tip<\/strong>: do not use &#8220;<strong>textLabel<\/strong>&#8220;, &#8220;<strong>detailTextLabel<\/strong>&#8221; variables in your custom <strong>UITableViewCell<\/strong> classes, because they will interrupt with native <strong>textLabel<\/strong> and you will get <strong>SIGABRT<\/strong> bug in runtime. <\/p>\n<p>P.S.S. If frames are drawn incorrectly, try to override layoutSubviews method of your custom class or remove autoresizing masks. This is another topic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is a very fast and convenient way to create a custom UITableViewCell layout using Interface Builder. But Apple for some reason has not made this easy &#8211; you have to know some magic things to perform this task. There is a a ton of tutorials on this topic already. But nevertheless I decided to [&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-1795","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>Instructions on how to create custom UITableViewCell with a xib - 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=1795\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Instructions on how to create custom UITableViewCell with a xib - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"It is a very fast and convenient way to create a custom UITableViewCell layout using Interface Builder. But Apple for some reason has not made this easy &#8211; you have to know some magic things to perform this task. There is a a ton of tutorials on this topic already. But nevertheless I decided to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1795\" \/>\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=\"2013-09-24T09:39:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-09-24T16:20:13+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Instructions on how to create custom UITableViewCell with a xib\",\"datePublished\":\"2013-09-24T09:39:13+00:00\",\"dateModified\":\"2013-09-24T16:20:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795\"},\"wordCount\":342,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795\",\"name\":\"Instructions on how to create custom UITableViewCell with a xib - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2013-09-24T09:39:13+00:00\",\"dateModified\":\"2013-09-24T16:20:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1795#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Instructions on how to create custom UITableViewCell with a xib\"}]},{\"@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":"Instructions on how to create custom UITableViewCell with a xib - 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=1795","og_locale":"en_US","og_type":"article","og_title":"Instructions on how to create custom UITableViewCell with a xib - Denis Kutlubaev","og_description":"It is a very fast and convenient way to create a custom UITableViewCell layout using Interface Builder. But Apple for some reason has not made this easy &#8211; you have to know some magic things to perform this task. There is a a ton of tutorials on this topic already. But nevertheless I decided to [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1795","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2013-09-24T09:39:13+00:00","article_modified_time":"2013-09-24T16:20:13+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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1795#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1795"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Instructions on how to create custom UITableViewCell with a xib","datePublished":"2013-09-24T09:39:13+00:00","dateModified":"2013-09-24T16:20:13+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1795"},"wordCount":342,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1795#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1795","url":"https:\/\/blog.alwawee.ru\/?p=1795","name":"Instructions on how to create custom UITableViewCell with a xib - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2013-09-24T09:39:13+00:00","dateModified":"2013-09-24T16:20:13+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1795#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1795"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1795#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Instructions on how to create custom UITableViewCell with a xib"}]},{"@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\/1795","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=1795"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1795\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}