UITableView Multiple options
Customize the cell and take it to the appropriate cell.
TableViewCell.h
#import <UIKit/UIKit.h> @interface Tableviewcell:uitableviewcell { BOOL _checked; Uiimageview *_checkedimage;} -(void) setchecked: (BOOL) checked; @end
tableviewcell.m
#import "TableViewCell.h" @implementation tableviewcell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) Style Reuseidentifier: (NSString *) reuseidentifier {self = [super Initwithstyle:style Reuseidentifier:reuseidentifier] ; if (self) {_checkedimage = [[Uiimageview alloc]init]; _checkedimage.image = [UIImage imagenamed:@ "unselected"]; [Self.contentview Addsubview:_checkedimage]; } return self;} -(void) layoutsubviews {[Super layoutsubviews]; _checkedimage.frame = CGRectMake (10, 10, 29, 29);} -(void) setchecked: (BOOL) checked {if (checked) {_checkedimage.image = [UIImage imagenamed:@ "Selected"]; }else {_checkedimage.image = [UIImage imagenamed:@ "unselected"]; }
_checked = checked;} -(void) awakefromnib {//initialization code}-(void) setselected: (BOOL) selected animated: (bool) animated {[Super SE Tselected:selected animated:animated]; Configure The view for the selected state} @end
VIEWCONTROLLER.M
#import "ViewController.h" #import "TableViewCell.h" @interface Viewcontroller () <uitableviewdatasource, uitableviewdelegate> {UITableView *_tableview;} @property (Nonatomic,strong) Nsmutablearray *array; @property (nonatomic,strong) Nsmutablearray *checkedarray;@ End@implementation viewcontroller-(void) viewdidload {[Super viewdidload]; [Self initdatasource]; Self.view.backgroundColor = [Uicolor Lightgraycolor]; _tableview = [[UITableView alloc]initwithframe:cgrectmake (0, Cgrectgetwidth (self.view.bounds), Cgrectgetheight ( Self.view.bounds)]; _tableview.datasource = self; _tableview.delegate = self; [Self.view Addsubview:_tableview]; UIButton *button = [UIButton Buttonwithtype:uibuttontypesystem]; [Button settitle:@ "Select All" forstate:uicontrolstatenormal]; [Button settitle:@ "Cancel" forstate:uicontrolstateselected]; Button.frame = CGRectMake (10, 10, 100, 50); [Button addtarget:self action: @selector (buttonpressed:) Forcontrolevents:uicontroleventtoUchupinside]; [Self.view Addsubview:button]; }-(void) InitDataSource {_checkedarray = [Nsmutablearray array]; for (int i = 0; i < self.array.count; i + +) {nsmutabledictionary *dic = [Nsmutabledictionary dictionary]; [DiC setvalue:@ "NO" forkey:@ "checked"]; [_checkedarray Addobject:dic]; }} #pragma mark-lazy load-(Nsmutablearray *) array {if (!_array) {_array = [[Nsmutablearray alloc]initwithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", nil]; } return _array;} #pragma mark-Event listener-(void) buttonpressed: (UIButton *) Sender {sender.selected =!sender.selected; Nsarray *anarrayofindexpath = [Nsarray arraywitharray:[_tableview indexpathsforvisiblerows]; for (int i = 0; i < [Anarrayofindexpath count]; i++) {Nsindexpath *indexpath= [Anarrayofindexpath objectatindex : i]; Get the corresponding cell Tableviewcell *cell = (tableviewcell*) [_tableview Cellforrowatindexpath:indexpath]; Nsuinteger row = [Indexpath row]; NsmutabledIctionary *dic = [_checkedarray objectatindex:row]; if (sender.selected) {[dic setobject:@ "YES" forkey:@ "checked"]; [Cell Setchecked:yes]; }else {[dic setobject:@ "NO" forkey:@ "checked"]; [Cell Setchecked:no]; }}} #pragma mark-<uitableviewdatasource,uitableviewdelegate>-(Nsinteger) Numberofsectionsintableview: ( UITableView *) TableView {return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section {return self.array.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {static Nsstri ng *cellid = @ "Cellid"; Tableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellid]; if (!cell) {cell = [[Tableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } nsuinteger row = Indexpath.row; [Self Cellchecked:cell row:row isselected:no]; return cell;} -(VOID) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {[TableView Deselectrowatindexpath:indexpath Animated:yes]; Tableviewcell *cell = (tableviewcell*) [TableView Cellforrowatindexpath:indexpath]; Nsuinteger row = Indexpath.row; [Self Cellchecked:cell row:row isselected:yes];} #pragma mark-others/** * Click, and when loading the cell, make a decision to change the cell's selected state * * @param cell Custom cell * @param row TableView Subscript * @param isSelected is click */-(void) cellchecked: (Tableviewcell *) cell row: (Nsuinteger) Row isSelected: (BOOL) isselected{nsmutabledictionary *dic = [_checkedarray objectatindex:row]; if ([[DiC objectforkey:@ "checked"] isequaltostring:@ "NO"]) {if (isSelected) {[dic setobject:@ ' YES ' for key:@ "Checked"]; [Cell Setchecked:yes]; }else {[dic setobject:@ "NO" forkey:@ "checked"]; [Cell Setchecked:no]; }}else {if (!isselected) {[dic setobject:@ "yes" Forkey:@ "Checked"]; [Cell Setchecked:yes]; }else {[dic setobject:@ "NO" forkey:@ "checked"]; [Cell Setchecked:no]; } }}
:
iOS Development--ui OC Chapter &uitableview Multiple options