Delete IOS UITableView and iosuitableview

Source: Internet
Author: User

Delete IOS UITableView and iosuitableview

UITbableView is used as the list display information. In addition to the display function, it is sometimes used for deletion, such as a shopping cart. You can directly use the system-provided delete function to delete the current cell. when the cell is scanned horizontally, the red delete button appears on the right side and click Delete.

 

To use the built-in deletion function, follow these steps:

1. Set the editing of tableView to YES.

2. Return to the edit mode, that is, to implement tableview: editingStyleForRowAtIndexPath: Method in UITableViewDelegate, and return the delete mode in it. If not, the deletion mode is returned by default.

3. Submit the delete operation to implement tableview: commitEditingStyle: editing StyleForRowAtIndexPath: method. As long as this method is implemented, the delete method of the delete button is implemented by default.

4. If you want to change the delete button to Chinese, you can implement the tableView: titleForDeleteConfirmationButtonForRowAtIndexPath method.

Code:

// ViewController. m // JRTableView Delete /// Created by jerehedu on 15/6/11. // Copyright (c) 2015 jerehedu. all rights reserved. // # import "ViewController. h "# import" Goods. h "@ interface ViewController () <UITableViewDataSource, UITableViewDelegate> {UITableView * _ tableView; // list NSMutableArray * _ goodsAry; // product array UIButton * _ editBtn; // edit button} @ end @ implementation ViewController-(void) viewDidLoad {[super view DidLoad]; // Add the title UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (0, 20, self. view. frame. size. width, 44)]; titleLabel. text = @ "Shopping Cart"; titleLabel. textAlignment = NSTextAlignmentCenter; titleLabel. backgroundColor = [UIColor redColor]; titleLabel. textColor = [UIColor whiteColor]; [self. view addSubview: titleLabel]; // Add the edit button _ editBtn = [UIButton buttonWithType: UIButtonTypeCustom]; _ editBt N. frame = CGRectMake (self. view. frame. size. width-60, 25, 50, 34); [_ editBtn setTitle: @ "edit" forState: UIControlStateNormal]; [_ editBtn setTitle: @ "complete" forState: UIControlStateSelected]; _ editBtn. titleLabel. font = [UIFont systemFontOfSize: 15]; _ editBtn. backgroundColor = [UIColor colorWithRed: 0.8 green: 0.8 blue: 0.8 alpha: 0.5]; [self. view addSubview: _ editBtn]; [_ editBtn addTarget: self action: @ selector (cli CkEditBtn :) forControlEvents: UIControlEventTouchUpInside]; // Add tableview _ tableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 64, self. view. frame. size. width, self. view. frame. size. height-64)]; _ tableView. dataSource = self; _ tableView. delegate = self; [self. view addSubview: _ tableView]; // obtain data NSArray * ary = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "ShoppingGoo DsList "ofType: @" plist "]; // Save the data to the model object, and then save the object to the array _ goodsAry = [NSMutableArray array]; for (int I = 0; I <ary. count; I ++) {Goods * good = [Goods goodsWithDic: ary [I]; [_ goodsAry addObject: good];} # The pragma mark data source returns several rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return _ goodsAry. count ;}# content displayed on each row of The pragma mark-(UITableViewCell *) tableView :( UITableView *) tableVie W cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * idGood = @ "goods"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: idGood]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: idGood];} Goods * good = _ goodsAry [indexPath. row]; cell. imageView. image = [UIImage imageNamed: good. icon]; cell. textLabel. text = Good. name; cell. detailTextLabel. text = good. details; cell. detailTextLabel. numberOfLines = 6; cell. detailTextLabel. textColor = [UIColor brownColor]; return cell ;}# pragma mark selected row-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// deselect the selected status [tableView deselectRowAtIndexPath: indexPath animated: YES];} # pragma mark sets the Row Height (CGFloat) tableView :( UITableView *) tableView h EightForRowAtIndexPath :( NSIndexPath *) indexPath {return 110;} # pragma mark click the edit button-(IBAction) clickEditBtn :( UIButton *) sender {// set the tableview editing status BOOL flag =! _ TableView. editing; [_ tableView setEditing: flag animated: YES]; _ editBtn. selected = flag;} # pragma mark returns the edit mode. The default mode is the delete mode-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {// return response; // return UITableViewCellEditingStyleInsert; return UITableViewCellEditingStyleDelete;} # pragma mark submit the edit operation-(void) tableVie W :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {// by default, sliding Delete is implemented !!!!! If (editingStyle! = UITableViewCellEditingStyleDelete) return; // Delete the data model [_ goodsAry removeObjectAtIndex: indexPath. row]; // refresh the interface // [_ tableView reloadData]; [_ tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];} # pragma mark delete button (NSString *) tableView :( UITableView *) tableView titleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *) indexPath {return @ "delete";} @ end ///Goods. h // delete a shopping cart table /// Created by jerei on 15-1-7. // Copyright (c) 2015 jerei. all rights reserved. // # import <Foundation/Foundation. h> @ interface Goods: NSObject @ property (nonatomic, copy) NSString * icon; @ property (nonatomic, copy) NSString * name; @ property (nonatomic, copy) NSString * details; -(id) initWithDic :( NSDictionary *) dic; + (id) goodsWithDic :( NSDictionary *) dic; @ end # import "Goods. h "@ implementation Goods-(id) initWithDic :( NSDictionary *) dic {if (self = [super init]) {self. icon = [dic objectForKey: @ "icon"]; self. name = [dic objectForKey: @ "name"]; self. details = [dic objectForKey: @ "details"];} return self;} + (id) goodsWithDic :( NSDictionary *) dic {Goods * good = [[Goods alloc] initWithDic: dic]; return good ;}@ end

 

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

Related Article

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.