uitableview - Creating an editable Table View in iOS? -
i trying create editable table view in ios using .xib files.
my code looks :
viewcontroller.h
#import <uikit/uikit.h> @interface viewcontroller : uitableviewcontroller @property (nonatomic, strong) nsmutablearray *notes; @end
viewcontroller.m
#import "viewcontroller.h" #import "note.h" @interface viewcontroller () @end @implementation viewcontroller - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; [self setediting:no animated:no]; } - (void)viewwillappear:(bool)animated{ self.notes = [[note savednotes]mutablecopy]; [self.tableview reloaddata]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - button events - (void)setediting:(bool)editing animated:(bool)animated { [super setediting:editing animated:animated]; if (editing) { uibarbuttonitem *cancelbutton = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel target:self action:@selector(cancelbuttonpressed:)]; self.navigationitem.leftbarbuttonitem = cancelbutton; uibarbuttonitem *donebutton = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(rightbuttonpressed:)]; self.navigationitem.rightbarbuttonitem = donebutton; }else{ self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self action:@selector(addnotebuttonpressed:)]; uibarbuttonitem *editbutton = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemedit target:self action:@selector(rightbuttonpressed:)]; self.navigationitem.rightbarbuttonitem = editbutton; } } - (void)rightbuttonpressed:(id)sender{ [self setediting:!self.isediting animated:yes]; } - (void)cancelbuttonpressed:(id)sender{ [self setediting:!self.isediting animated:yes]; } - (void)addnotebuttonpressed:(id)sender{ //attviewcontroller *viewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"attviewcontroller"]; //[self.navigationcontroller pushviewcontroller:viewcontroller animated:yes]; } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return [self.notes count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // configure cell... note *note = self.notes[indexpath.row]; cell.textlabel.text = note.name; cell.detailtextlabel.text = note.event; return cell; } // override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } - (uitableviewcelleditingstyle)tableview:(uitableview *)tableview editingstyleforrowatindexpath:(nsindexpath *)indexpath { return uitableviewcelleditingstyledelete; } // override support editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete row data source note *note = self.notes[indexpath.row]; [note remove]; [self.notes removeobjectatindex:indexpath.row]; [tableview deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } else if (editingstyle == uitableviewcelleditingstyleinsert) { // create new instance of appropriate class, insert array, , add new row table view } } /* // override support rearranging table view. - (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)fromindexpath toindexpath:(nsindexpath *)toindexpath { } */ /* // override support conditional rearranging of table view. - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath { // return no if not want item re-orderable. return yes; } */ #pragma mark - table view delegate - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. /* <#detailviewcontroller#> *detailviewcontroller = [[<#detailviewcontroller#> alloc] initwithnibname:@"<#nib name#>" bundle:nil]; // ... // pass selected object new view controller. [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; */ [tableview deselectrowatindexpath:indexpath animated:yes]; } @end
in identity inspector of .xib , change class uiview uitableview .
i connect view datasource and/or delegate(i tried combinations).
however nothing on phone. dont table view , empty grey screen.
any ideas doing wrong? code above working in project using storyboards , trying make work .xib files.
screenshot :
i recommend checking out free sensible tableview framework. automatically handle displaying arrays , handle inserts/deletes on behalf. saves ton of time me.
Comments
Post a Comment