TableView Left slide button Tableviewcell custom left slide button

Source: Internet
Author: User

When we use TableView, we often need to show one or more buttons when the cell is left, but only one can be displayed by default, such as a common delete button, so what should we do when our requirements require multiple buttons?

First, now look at the system button (only one button is displayed)

Set the Delete button text after the cell left slide-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: ( Nsindexpath *) indexpath{
return @ "delete";}Call this method after clicking the cell's Delete button-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (nonnull nsindexpath *) indexpath{
if (Editingstyle = = uitableviewcelleditingstyledelete) {
[Callrecordsarr RemoveObjectAtIndex:indexPath.row];
Data source Delete the corresponding element before tableview delete the corresponding cell
[TableView Deleterowsatindexpaths:[nsmutablearray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationautomatic];
[Nskeyedarchiver Archiverootobject:callrecordsarr Tofile:callrecordscachepath];
    }}If you need to display multiple buttons, refer to the following code (note: When we use the custom button, the system default method as above will lose effect)-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{
returntrue;}-(Nsarray *) TableView: (UITableView *) TableView Editactionsforrowatindexpath: (Nonnullnsindexpath *) indexpath{
uitableviewrowaction *action1 = [Uitableviewrowactionrowactionwithstyle:uitableviewrowactionstylenormaltitle: @ "Blacklist" handler:^ (Uitableviewrowaction * _nonnull action, Nsindexpath * _nonnull indexpath) {
Implementing the corresponding event in the block
}];
uitableviewrowaction *action2 = [Uitableviewrowactionrowactionwithstyle: uitableviewrowactionstyledestructivetitle:@ "Delete" handler:^ (uitableviewrowaction * _nonnull Action, NSIndexPath * _ Nonnull indexpath) {
[CallRecordsArrremoveObjectAtIndex:indexPath.row];
Data source Delete the corresponding element before tableview delete the corresponding cell
[TableView Deleterowsatindexpaths:[nsmutablearrayarraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationautomatic];
[NSKeyedArchiverarchiveRootObject:callRecordsArrtoFile:CALLRECORDSCACHEPATH];
    }];Action2.backgroundcolor = [Uicolorpurplecolor];Note: 1, when the value of Rowactionwithstyle is uitableviewrowactionstyledestructive, the system default background color is red ; When the value is Uitableviewrowactionstylenormal, the background color defaults to light gray, which can be set by the. BackgroundColor of the Uitableviewrowaction object.2, when the left slide button to perform the operation involving the data source and the page updates, to update the data source, in the update view, otherwise there will be unresponsive situationuitableviewrowaction *totop = [Uitableviewrowactionrowactionwithstyle: uitableviewrowactionstyledestructivetitle:@ "pinned" handler:^ (uitableviewrowaction * _nonnull Action, NSIndexPath * _ Nonnull indexpath) {
//Update data
[callrecordsarrexchangeobjectatindex:indexpath.rowwithobjectatindex:0];
//Update page
Nsindexpath *firstindexpath = [nsindexpathindexpathforrow:0insection:indexpath.section];
[TableView Moverowatindexpath:indexpath Toindexpath:firstindexpath];
    }];Here the order in which the Uitableviewrowaction objects are placed determines the order of the corresponding buttons in the cell
[email protected][totop,action2,action1];}The display graph is as follows

One thing to add to this is that when I check the relevant information, the custom button above adds "

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath

{

} "This method, and I found in the experimental test did not have this method can also achieve the expected function, and also did not find any bugs, so this summary of the method is for reference only, if any god knows this, please enlighten!

TableView Left slide button Tableviewcell custom left slide button

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.