UITableViewCell edit mode Customize the Picture button on the left

Source: Internet
Author: User
Tags uikit

First, let's take a look at an effect chart that we're going to do today:

We want to implement the edit mode, the cell moves to the right, showing the delete button.
Looking for a long time on the internet, did not find a solution, the Internet is to modify the image of multiple selection mode, the theory is nothing more than to facilitate the bottom cell, to find more than the selected ImageView and then replace the picture. To achieve this effect, the simplest thing is to find ImageView in the Add mode. But the reality tells me, is impossible. So you just have to define it yourself.

First, we implement the click into edit mode inside the TableView:

-(void) viewdidload {[Super viewdidload];

    Self.view.backgroundcolor=jycolor (240, 239, 249, 1);

    self.navigationitem.title=@ "timing Switch"; Self.navigationitem.rightbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "edit" style:
    Uibarbuttonitemstyledone target:self Action: @selector (Rightbarbtnclick:)];

[Self setupsubviews]; }-(void) Rightbarbtnclick: (Uibarbuttonitem *) btn {if ([Btn.title isequaltostring:@ edit]) {}else{} SE
    lf.tableview.editing=!self.tableview.editing;
Btn.title=[btn.title isequaltostring:@ "edit"]?@ "Cancel": @ "edit";
    }-(void) setupsubviews {uitableview *tableview=[[uitableview alloc]init];
    Tableview.backgroundcolor=jycolor (240, 239, 249, 1);
    Self.tableview=tableview;
    [Self.tableview Settablefooterview:[[uiview Alloc]initwithframe:cgrectzero]];
    [Self.view Addsubview:tableview];
    tableview.delegate=self;
    tableview.datasource=self;
    Tableview.translatesautoresizingmaskintoconstraints=no; [TableView AutopineDgetosuperviewedge:aledgeleading];
    [TableView autopinedgetosuperviewedge:aledgetrailing];
    [TableView Autopinedgetosuperviewedge:aledgetop];

[TableView Autopinedgetosuperviewedge:aledgebottom withinset:115];

    self.tableview.allowsMultipleSelectionDuringEditing = YES;
    UIButton *addbtn=[[uibutton Alloc]init];
    Addbtn.backgroundcolor=[uicolor Bluecolor];
    [Self.view ADDSUBVIEW:ADDBTN];
    [Addbtn autopinedge:aledgetop toedge:aledgebottom Ofview:tableview withoffset:10];
    [Addbtn autosetdimension:aldimensionwidth tosize:50];
    [Addbtn autosetdimension:aldimensionheight tosize:50];
[Addbtn autoalignaxistosuperviewaxis:alaxisvertical];

 }

And then we started to build the cell model and model.

TimeSwitchModel.h file:

#import <Foundation/Foundation.h>

@interface timeswitchmodel:nsobject
@property (nonatomic,copy) NSString *idstr;
@property (nonatomic,copy) nsstring *namestr;
@property (nonatomic,copy) nsstring *datastr;
@end

TimeSwitchCell.h file

#import <UIKit/UIKit.h>
@class Timeswitchmodel;

@interface Timeswitchcell:uitableviewcell
@property (nonatomic,strong) Timeswitchmodel *model;
@property (nonatomic,copy) void (^deleteclick) (NSString *);
@end

TIMESWITCHCELL.M file

#import "TimeSwitchCell.h" #import "TimeSwitchModel.h" @interface Timeswitchcell () {Uiimageview *_headpicimageview;
    Uilabel *_namelabel;
    Uilabel *_datelabel;
UIButton *_deletebtn; @end @implementation Timeswitchcell-(void) awakefromnib {}-(Instancetype) Initwithstyle: (Uitableviewcellstyle) sty Le Reuseidentifier: (NSString *) Reuseidentifier {if (self = [Super Initwithstyle:style Reuseidentifier:reuseidentifier
    ] {[Self initview];
return self;
    }-(void) Initview {_headpicimageview=[[uiimageview alloc]init];

    [Self.contentview Addsubview:_headpicimageview];
    _deletebtn=[uibutton Buttonwithtype:uibuttontypecustom];

    [Self.contentview ADDSUBVIEW:_DELETEBTN];
    _namelabel=[[uilabel Alloc]init];

    [Self.contentview Addsubview:_namelabel];
    _datelabel=[[uilabel Alloc]init];
[Self.contentview Addsubview:_datelabel];

    }-(void) Setmodel: (Timeswitchmodel *) model {_model=model; _deletebtn.frame=cgrectmake (-79, 0, 79, 80);
    [_deletebtn setimage:[uiimage imagenamed:@ "Delete_icon.png"] forstate:uicontrolstatenormal];
    [_deletebtn setimage:[uiimage imagenamed:@ "Delete_highlight_icon.png"] forstate:uicontrolstatehighlighted];

    [_deletebtn addtarget:self Action: @selector (Deletebtnclick:) forcontrolevents:uicontroleventtouchupinside];
    _headpicimageview.image=[uiimage imagenamed:@ "Timeswich_head_icon.png"];

    _headpicimageview.frame=cgrectmake (8, 12, 56, 56);
    _namelabel.font=[uifont Systemfontofsize:17];
    _namelabel.textcolor=jycolor (109, 109, 109, 1);
    _namelabel.frame=cgrectmake (72, 16, 300, 30);

    _NAMELABEL.TEXT=MODEL.NAMESTR;
    _datelabel.font=[uifont systemfontofsize:15];
    _datelabel.textcolor=jycolor (255, 128, 3, 1);
    _datelabel.frame=cgrectmake (72, 50, 300, 30);

_DATELABEL.TEXT=MODEL.DATASTR;
    }-(void) Deletebtnclick: (UIButton *) idstr {if (Self.deleteclick) {Self.deleteclick (_MODEL.IDSTR); }}-(void) setediting: (BOOL) Editing animateD: (BOOL) animated{if (self.editing = = editing) {return;
} [Super Setediting:editing animated:animated]; CGFloat movespace = 79-38;//cell edit mode will help you move automatically 38 so we'll move on. 79-38 if (self.editing) {[_headpicimagevie W Setframe:cgrectmake (_headpicimageview.frame.origin.x + Movespace, _HEADPICIMAGEVIEW.FRAME.ORIGIN.Y, _
        HeadpicImageview.frame.size.width, _headpicimageview.frame.size.height)]; [_deletebtn setframe:cgrectmake (_deletebtn.frame.origin.x + Movespace, _DELETEBTN.FRAME.ORIGIN.Y, _
        Deletebtn.frame.size.width, _deletebtn.frame.size.height)]; [_namelabel setframe:cgrectmake (_namelabel.frame.origin.x + Movespace, _NAMELABEL.FRAME.ORIGIN.Y, _
        Namelabel.frame.size.width, _namelabel.frame.size.height)]; [_datelabel setframe:cgrectmake (_datelabel.frame.origin.x + Movespace, _DATELABEL.FRAME.ORIGIN.Y, _

    Datelabel.frame.size.width, _datelabel.frame.size.height)]; }else {[_headpicimageview setframe:cgrectmake (_heaDpicimageview.frame.origin.x-movespace, _HEADPICIMAGEVIEW.FRAME.ORIGIN.Y, _headpicimageview.frame.size.width, _
        HeadpicImageview.frame.size.height)]; [_deletebtn Setframe:cgrectmake (_deletebtn.frame.origin.x-movespace, _DELETEBTN.FRAME.ORIGIN.Y, _
        Deletebtn.frame.size.width, _deletebtn.frame.size.height)]; [_namelabel Setframe:cgrectmake (_namelabel.frame.origin.x-movespace, _NAMELABEL.FRAME.ORIGIN.Y, _
        Namelabel.frame.size.width, _namelabel.frame.size.height)]; [_datelabel Setframe:cgrectmake (_datelabel.frame.origin.x-movespace, _DATELABEL.FRAME.ORIGIN.Y, _
    Datelabel.frame.size.width, _datelabel.frame.size.height)];
 }
}

In Uitableviw, we use a block to enter the model ID of which cell we clicked.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {

    static NSString *identifier=@ "Timewsitch";
    Timeswitchcell *cell = [TableView dequeuereusablecellwithidentifier:identifier];

    if (cell==nil) {
        Cell=[[timeswitchcell alloc]initwithstyle:uitableviewcellstyledefault reuseIdentifier: identifier];
    }
    Timeswitchmodel *model=[[timeswitchmodel Alloc]init];
    model.idstr=[@ "stringbyappendingstring:[nsstring" stringwithformat:@ "%ld", (long) indexpath.row]];
    model.namestr=@ "Here is the name of the product";
    model.datastr=@ "2017/01/24";
    Cell.model=model;
    cell.deleteclick=^ (NSString * idstr) {
         NSLog (@ "-IDSTR is:%@", idstr);

    return cell;
}

OK, run it and see the effect:

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.