IOS tableview side-by-side Removal and mobile cell implementation

Source: Internet
Author: User
<span id="Label3"></p><p><p>Mu class online learning TableView use, Suddenly let me think iOS more simple than android, may be my feeling it. Because Android implements the list view side pull delete, The dynamic move item process is a little bit more complicated. But iOS just needs to rewrite a few ways to do it. I can only say that iOS is amazing! I followed it.<br>Project Address: Todo<br></p></p><p><p></p></p><p><p>Look at the effect, the UI is also available. First on the storyboard structure diagram:<br><br>Navigate controller implements a navigation bar. The view controller implements a Tableview,tableviewcell.</p></p><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">//viewcontroller.swift</span></span><span class="hljs-comment"><span class="hljs-comment">//Todo</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">//Created by Zhouyihua on 15/8/17.</span></span><span class="hljs-comment"><span class="hljs-comment">//Copyright (c) 2015 Xiebangyuan. All Rights Reserved.</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span>Import Uikitvar todos:[todomodel] = []var filtertodos:[todomodel] = []func datefromstring (datestr:string),<span class="hljs-built_in"><span class="hljs-built_in">NSDate</span></span>? {let dateformatter = NSDateFormatter () dateformatter<span class="hljs-variable"><span class="hljs-variable">. DateFormat</span></span>=<span class="hljs-string"><span class="hljs-string">"yyyy-mm-dd"</span></span>Let date = Dateformatter<span class="hljs-variable"><span class="hljs-variable">. datefromstring</span></span>(datestr)<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Date}class Viewcontroller:<span class="hljs-built_in"><span class="hljs-built_in">Uiviewcontroller</span></span>,<span class="hljs-built_in"><span class="hljs-built_in">Uitableviewdatasource</span></span>,<span class="hljs-built_in"><span class="hljs-built_in">uitableviewdelegate</span></span>, Uisearchdisplaydelegate{@IBOutlet<span class="hljs-keyword"><span class="hljs-keyword">Weak</span></span>var tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>! Override Func Viewdidload () {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span><span class="hljs-variable"><span class="hljs-variable">. Viewdidload</span></span>()<span class="hljs-comment"><span class="hljs-comment">additional setup after loading the view, typically from a nib.</span></span>Todos = [todomodel (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>:<span class="hljs-string"><span class="hljs-string">"1"</span></span>, image:<span class="hljs-string"><span class="hljs-string">"child-selected"</span></span>, title:<span class="hljs-string"><span class="hljs-string">"1. Go to the amusement park</span></span>, Date:datefromstring (<span class="hljs-string"><span class="hljs-string">"2014-10-20"</span></span>)!), Todomodel (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>:<span class="hljs-string"><span class="hljs-string">"2"</span></span>, image:<span class="hljs-string"><span class="hljs-string">"shopping-cart-selected"</span></span>, title:<span class="hljs-string"><span class="hljs-string">"2. Shopping "</span></span>, Date:datefromstring (<span class="hljs-string"><span class="hljs-string">"2014-10-28"</span></span>)!), Todomodel (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>:<span class="hljs-string"><span class="hljs-string">"3"</span></span>, image:<span class="hljs-string"><span class="hljs-string">"phone-selected"</span></span>, title:<span class="hljs-string"><span class="hljs-string">"3. Call "</span></span>, Date:datefromstring (<span class="hljs-string"><span class="hljs-string">"2014-10-30"</span></span>)!), Todomodel (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>:<span class="hljs-string"><span class="hljs-string">"4"</span></span>, image:<span class="hljs-string"><span class="hljs-string">"travel-selected"</span></span>, title:<span class="hljs-string"><span class="hljs-string">"4. Travel to Europe "</span></span>, Date:datefromstring (<span class="hljs-string"><span class="hljs-string">"2014-10-31"</span></span>)!)] Navigationitem<span class="hljs-variable"><span class="hljs-variable">. Leftbarbuttonitem</span></span>= Editbuttonitem () var contentoffset = TableView<span class="hljs-variable"><span class="hljs-variable">. Contentoffset</span></span>Contentoffset<span class="hljs-variable"><span class="hljs-variable">. Y</span></span>+ = searchdisplaycontroller!<span class="hljs-variable"><span class="hljs-variable">. Searchbar</span></span><span class="hljs-variable"><span class="hljs-variable">. Frame</span></span><span class="hljs-variable"><span class="hljs-variable">. Size</span></span><span class="hljs-variable"><span class="hljs-variable">. Height</span></span>TableView<span class="hljs-variable"><span class="hljs-variable">. Contentoffset</span></span>= contentoffset} override func didreceivememorywarning () {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span><span class="hljs-variable"><span class="hljs-variable">. didreceivememorywarning</span></span>()<span class="hljs-comment"><span class="hljs-comment">//Dispose of any resources, can be Recreated.</span></span>}<span class="hljs-comment"><span class="hljs-comment">//returns the number of loaded bars method from Uitableviewdelegate</span></span>Func TableView (tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>, Numberofrowsinsection section:int), int{<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>TableView = = searchdisplaycontroller?<span class="hljs-variable"><span class="hljs-variable">. Searchresultstableview</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Filtertodos<span class="hljs-variable"><span class="hljs-variable">. Count</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Todos<span class="hljs-variable"><span class="hljs-variable">. Count</span></span>} }<span class="hljs-comment"><span class="hljs-comment">//Row Display. Implementers should *always* try to reuse cells by setting each Cell's reuseidentifier and querying for available reusable Cells with Dequeuereusablecellwithidentifier:</span></span> <span class="hljs-comment"><span class="hljs-comment">//Cell gets various attributes set automatically based on table (separators) and data source (accessory views, Editin G Controls)</span></span> <span class="hljs-comment"><span class="hljs-comment">//the cell that returns TableView is the item of List view</span></span>Func TableView (tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>, Cellforrowatindexpath Indexpath:<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>),<span class="hljs-built_in"><span class="hljs-built_in">UITableViewCell</span></span>{let cell =<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span><span class="hljs-variable"><span class="hljs-variable">. Dequeuereusablecellwithidentifier</span></span>(<span class="hljs-string"><span class="hljs-string">"todocell"</span></span>) as!<span class="hljs-built_in"><span class="hljs-built_in">UITableViewCell</span></span>var todo:todomodel?<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>TableView = = searchdisplaycontroller?<span class="hljs-variable"><span class="hljs-variable">. Searchresultstableview</span></span>{todo = Filtertodos[indexpath<span class="hljs-variable"><span class="hljs-variable">. Row</span></span>] as todomodel}<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{todo = Todos[indexpath<span class="hljs-variable"><span class="hljs-variable">. Row</span></span>] as todomodel} var title = cell<span class="hljs-variable"><span class="hljs-variable">. Viewwithtag</span></span>(<span class="hljs-number"><span class="hljs-number">103</span></span>) as!<span class="hljs-built_in"><span class="hljs-built_in">UILabel</span></span>var date = cell<span class="hljs-variable"><span class="hljs-variable">. Viewwithtag</span></span>(<span class="hljs-number"><span class="hljs-number">104</span></span>) as!<span class="hljs-built_in"><span class="hljs-built_in">UILabel</span></span>var image = Cell<span class="hljs-variable"><span class="hljs-variable">. Viewwithtag</span></span>(<span class="hljs-number"><span class="hljs-number">102</span></span>) as!<span class="hljs-built_in"><span class="hljs-built_in">Uiimageview</span></span>Image<span class="hljs-variable"><span class="hljs-variable">. Image</span></span>=<span class="hljs-built_in"><span class="hljs-built_in">UIImage</span></span>(named:todo!<span class="hljs-variable"><span class="hljs-variable">. Image</span></span>) title<span class="hljs-variable"><span class="hljs-variable">. Text</span></span>= todo!<span class="hljs-variable"><span class="hljs-variable">. Title</span></span>Let locale = Nslocale<span class="hljs-variable"><span class="hljs-variable">. Currentlocale</span></span>() Let DateFormat = NSDateFormatter<span class="hljs-variable"><span class="hljs-variable">. Dateformatfromtemplate</span></span>(<span class="hljs-string"><span class="hljs-string">"yyyy-mm-dd"</span></span>, options:<span class="hljs-number"><span class="hljs-number">0</span></span>, Locale:locale) let dateformatter = NSDateFormatter () dateformatter<span class="hljs-variable"><span class="hljs-variable">. DateFormat</span></span>= DateFormat Date<span class="hljs-variable"><span class="hljs-variable">. Text</span></span>= Dateformatter<span class="hljs-variable"><span class="hljs-variable">. Stringfromdate</span></span>(todo!<span class="hljs-variable"><span class="hljs-variable">. Date</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>cell}<span class="hljs-comment"><span class="hljs-comment">//mark-uitableviewdelegate</span></span>Func TableView (tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>, Commiteditingstyle editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexPath:<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>){<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>Editingstyle = = Uitableviewcelleditingstyle<span class="hljs-variable"><span class="hljs-variable">. Delete</span></span>{todos<span class="hljs-variable"><span class="hljs-variable">. Removeatindex</span></span>(indexpath<span class="hljs-variable"><span class="hljs-variable">. Row</span></span>)<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span><span class="hljs-variable"><span class="hljs-variable">. Deleterowsatindexpaths</span></span>([indexpath],withrowanimation:uitableviewrowanimation<span class="hljs-variable"><span class="hljs-variable">. Automatic</span></span>}}} override func setediting (editing:bool, Animated:bool) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span><span class="hljs-variable"><span class="hljs-variable">. setediting</span></span>(editing, Animated:animated)<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span><span class="hljs-variable"><span class="hljs-variable">. setediting</span></span>(editing, animated:animated)}<span class="hljs-comment"><span class="hljs-comment">//click OK button unsegue action</span></span>@IBAction func close (segue:uistoryboardsegue) {println (<span class="hljs-string"><span class="hljs-string">"close"</span></span>) TableView<span class="hljs-variable"><span class="hljs-variable">. Reloaddata</span></span>() }<span class="hljs-comment"><span class="hljs-comment">//implement slide-slip removal by uitableviewdelegate</span></span>Func TableView (tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>, Canmoverowatindexpath Indexpath:<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>)-bool{<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>editing}<span class="hljs-comment"><span class="hljs-comment">//implement cell move, method from Uitableviewdelegate</span></span>Func TableView (tableView:<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>, Moverowatindexpath Sourceindexpath:<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>, Toindexpath Destinationindexpath:<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>) {let todo = Todos<span class="hljs-variable"><span class="hljs-variable">. Removeatindex</span></span>(sourceindexpath<span class="hljs-variable"><span class="hljs-variable">. Row</span></span>) Todos<span class="hljs-variable"><span class="hljs-variable">. Insert</span></span>(todo, Atindex:destinationindexpath<span class="hljs-variable"><span class="hljs-variable">. Row</span></span>) }}</code></pre><p><p>The realization of TableView is obviously given to a datasoure and a Delegate. View controller extension uitableviewdatasource (processing data source), uitableviewdelegate (handles various operations Tableview)</p></p><p><p>finally, remember to bind Datasoure and Delegate. Seems to be not very simple. oh, my, Android Program Ape's passing points like!!</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>IOS tableview side-by-side Removal and mobile cell implementation</p></p></span>

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.