IOS: deletion of multiple cells (method two):

Source: Internet
Author: User
<span id="Label3"></p><p><p> <span style="color: #ff0000; font-size: 16px;">Here's a Foolproof method one, which describes the second way to delete a cell by deleting the contents of the Cell: (but this situation has a small disadvantage, due to the cell reuse mechanism, if the cell content is the same, the presence of the tag will cause a false deletion)</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;"><span style="color: #ff00ff;">before deletion</span> :</span></p></p><p><p><span style="color: #ff0000; font-size: 16px;"></span></p></p><p><p></p></p><p><p><span style="color: #ff00ff; font-size: 16px;">After deletion:</span></p></p><p><p></p></p><p><p> <span style="color: #ff00ff; font-size: 18px;">The analysis is as Follows: (if the contents of each cell are different) how to delete the contents of the cell is a simple way, then how to implement the deletion of multiple cells?</span></p></p><p><p><span style="color: #ff00ff; font-size: 18px;">first, define two necessary mutable arrays, one to store the initialized raw data, and the other to store the selected cells, and then extract the data from it;</span></p></p><p><p><span style="color: #ff00ff; font-size: 18px;">secondly, The raw data is displayed in the table through the method of the data source, and by the method of the proxy, that is, the processing of the selected cell, to add a guideline view (tag) to the selected cell, and the contents of the first selected cell to be stored in the array, (two times Select to unmark it and remove it from the array);</span></p></p><p><p><span style="color: #ff00ff; font-size: 18px;">finally, the original data array deletes all the selected cell contents, while the data is checked and the array is emptied directly, then the table is refreshed as a whole.</span></p></p><p><p></p></p><p><p><span style="color: #ff00ff; font-size: 18px;">The code is as Follows:</span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>NUM 20<span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span>Viewcontroller () <UITableViewDataSource,UITableViewDelegate><span style="color: #008080;"><span style="color: #008080;">5</span></span>@property (weak, Nonatomic) Iboutlet UITableView *<span style="color: #000000;"><span style="color: #000000;">tableView;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>@property (strong,nonatomic) Nsmutablearray *products;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Raw Inventory of data</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>@property (strong,nonatomic) Nsmutablearray *productstore;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Selected Data Inventory</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>-(ibaction) deletebuttonclicked: (uibarbuttonitem *<span style="color: #000000;"><span style="color: #000000;">) sender;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>-(ibaction) deletebuttonclicked: (uibarbuttonitem *<span style="color: #000000;"><span style="color: #000000;">) Sender</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Remove all selected products from the original inventory</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>[self.productstore enumerateobjectsusingblock:^ (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>obj, Nsuinteger idx, BOOL *<span style="color: #000000;"><span style="color: #000000;">STOP) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[self.products removeobject:obj];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Clear the selected data inventory</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">[self.productstore removeallobjects];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Overall Refresh table</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[self.tableview reloaddata];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[super viewdidload];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Initialize</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span>Self.products =<span style="color: #000000;"><span style="color: #000000;">[nsmutablearray arraywithcapacity:num];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Self.productstore =<span style="color: #000000;"><span style="color: #000000;">[nsmutablearray arraywithcapacity:num];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<num; i++<span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>NSString *product = [nsstring stringwithformat:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">product-%02d</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, i];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">[self.products addobject:product];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set up data sources and proxies</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Self.tableView.dataSource =<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Self.tableview.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Data source methods for Mark-tableview<span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">how many row of each scetion</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>-(nsinteger) TableView: (uitableview *<span style="color: #000000;"><span style="color: #000000;">) TableView numberofrowsinsection: (nsinteger)</span> section</span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">self.products.count;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the contents of each cell</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) Indexpath</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. According to reuseidentifier, first go to the object pool to find the reused Cell object</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>NSString *reuseidentifier =<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Productcell</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>UITableViewCell *cell =<span style="color: #000000;"><span style="color: #000000;">[self.tableview dequeuereusablecellwithidentifier:reuseidentifier];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. If not found, create a Cell object yourself</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(cell = =<span style="color: #000000;"><span style="color: #000000;">Nil)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>Cell =<span style="color: #000000;"><span style="color: #000000;">[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Set the contents of a Cell object</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cell.textLabel.text =<span style="color: #000000;"><span style="color: #000000;">self.products[indexpath.row];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Font Color</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Cell.textLabel.textColor =<span style="color: #000000;"><span style="color: #000000;">[uicolor redcolor];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Font size</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Cell.textLabel.font = [uifont systemfontofsize:<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Cell color</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Cell.tintcolor =<span style="color: #000000;"><span style="color: #000000;">[uicolor orangecolor];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([self.productstore Containsobject:self.products[indexpath.row])<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">First selected</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add tag Display</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span>Cell.accessorytype =<span style="color: #000000;"><span style="color: #000000;">uitableviewcellaccessorycheckmark;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">two times selected</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Unmark Display</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Cell.accessorytype =<span style="color: #000000;"><span style="color: #000000;">uitableviewcellaccessorynone;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cell;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;">Bayi</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Proxy methods for Mark-tableview<span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">processing when a cell is selected</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) tableView: (uitableview *) tableView didselectrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) Indexpath</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the currently selected cell</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>UITableViewCell *cell =<span style="color: #000000;"><span style="color: #000000;">[tableView cellforrowatindexpath:indexpath];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Remove the product from the cell</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>NSString *product =<span style="color: #000000;"><span style="color: #000000;">self.products[indexpath.row];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add an auxiliary guide view to the selected cells and store the product in an array</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([self.productstore containsobject:product])<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">has been selected once</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">94</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Cancellation Token</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Cell.accessorytype =<span style="color: #000000;"><span style="color: #000000;">uitableviewcellaccessorynone;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">remove a product from the storage array</span></span><span style="color: #008080;"><span style="color: #008080;">98</span></span> <span style="color: #000000;"><span style="color: #000000;">[self.productstore removeobject:product];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">First select</span></span><span style="color: #008080;"><span style="color: #008080;">101</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">102</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add Tag</span></span><span style="color: #008080;"><span style="color: #008080;">103</span></span>Cell.accessorytype =<span style="color: #000000;"><span style="color: #000000;">uitableviewcellaccessorycheckmark;</span></span><span style="color: #008080;"><span style="color: #008080;">104</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add a product to the storage array</span></span><span style="color: #008080;"><span style="color: #008080;">106</span></span> <span style="color: #000000;"><span style="color: #000000;">[self.productstore addobject:product];</span></span><span style="color: #008080;"><span style="color: #008080;">107</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">108</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">109</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p><p>IOS: deletion of multiple cells (method two):</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.