<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Berkay Oruç</title>
    <description>The latest articles on DEV Community by Berkay Oruç (@berkayoruc).</description>
    <link>https://dev.to/berkayoruc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F768396%2Fdc87350b-2ffc-4d3d-82b1-fb60ea5ab96a.jpeg</url>
      <title>DEV Community: Berkay Oruç</title>
      <link>https://dev.to/berkayoruc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/berkayoruc"/>
    <language>en</language>
    <item>
      <title>Swipe actions in UITableViewCell</title>
      <dc:creator>Berkay Oruç</dc:creator>
      <pubDate>Mon, 04 Jul 2022 08:00:15 +0000</pubDate>
      <link>https://dev.to/berkayoruc/swipe-actions-in-uitableviewcell-3db1</link>
      <guid>https://dev.to/berkayoruc/swipe-actions-in-uitableviewcell-3db1</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtsizyil3mxodt7ww3m6.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtsizyil3mxodt7ww3m6.jpeg" alt="Swipe in mail"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The feature that comes with iOS 11 can be defined as left to right (&lt;strong&gt;leading&lt;/strong&gt;) or right to left (&lt;strong&gt;trailing&lt;/strong&gt;). Basically, we create swipe operations that we can give &lt;strong&gt;.normal&lt;/strong&gt; and &lt;strong&gt;.destructive&lt;/strong&gt; styles in UIContextualAction. We can create more than one UIContextualAction class that takes style, title and handler parameters and use it for the related swipe operation. Without further talking, let's write some code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqtts8n4jb6lsvkpa3dm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqtts8n4jb6lsvkpa3dm.gif" alt="Coding cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Code
&lt;/h2&gt;

&lt;p&gt;First, I assume we have already defined a UITableView. We use the following code to add a swipe action to the beginning of the UITableViewCell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -&amp;gt; UISwipeActionsConfiguration? { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the contrary, in order to add the swipe operation to the end of the UITableViewCell, we need to add the trailing state to our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -&amp;gt; UISwipeActionsConfiguration? { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's fill in these functions. I'm sure you've noticed. Our functions require optional UISwipeActionsConfiguration as a return type. Here, we can define multiple actions for trailing or leading swipe operations, thanks to the parameter type Swift provides us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -&amp;gt; UISwipeActionsConfiguration? {
   let favouriteaction = UIContextualAction(style: .normal, title: "Edit") { [weak self] (action, view, completionHandler) in
      self?.handleMarkAsFavourite()
      completionHandler(true)
   }
   favouriteaction.backgroundColor = .systemBlue
   let deleteaction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] (action, view, completionHandler) in
      self?.handleMoveToTrash(index: indexPath.row)
      completionHandler(true)
   }
   deleteaction.backgroundColor = .systemRed
   return UISwipeActionsConfiguration(actions: [favouriteaction, deleteaction])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8ja15ezmojqfqxi6ad5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8ja15ezmojqfqxi6ad5.gif" alt="Swipe result gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Last Words
&lt;/h2&gt;

&lt;p&gt;In this article, I tried to explain how we can define swipe operations that are indispensable for UITableViewCell. I hope this article will enrich your applications a little more. Happy days everyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://programmingwithswift.com/uitableviewcell-swipe-actions-with-swift/" rel="noopener noreferrer"&gt;Reference-1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.mobilhanem.com/ios-tableview-row-swipe-islemleri/" rel="noopener noreferrer"&gt;Reference-2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://material.io/components/lists#behavior" rel="noopener noreferrer"&gt;Reference-3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://berkayoruc.medium.com/uitableviewcellde-swipe-i%CC%87%C5%9Flemleri-e5a5802c46d4" rel="noopener noreferrer"&gt;Reference-4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>swift</category>
      <category>uitableviewcell</category>
      <category>ios</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
