Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

iphone - How to delete a row from UITableView -


i'm trying delete row table view , far have been unsuccessful. keep getting error:

"terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (5) must equal number of rows contained in section before update (5), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted).'" array use populate table view declared in class, objects array sqlite db.

the code used try deleting rows follows.

- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {  if (editingstyle == uitableviewcelleditingstyledelete) {     // delete row data source      [categoryarray objectatindex:indexpath.row];     [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:yes];  }    else if (editingstyle == uitableviewcelleditingstyleinsert) {     // create new instance of appropriate class, insert array, , add new row table view }    } 

now questions hope answered:

-how can delete rows properly?

-will face deleting row if 1 left in tableview?

-can change tableview used in method uitableview declared in .h file?

many thanks.


edit

-full .m code

#import "deletecategorytableview.h" #import "keycryptappappdelegate.h"   @implementation deletecategorytableview @synthesize categoryarray;  #pragma mark - #pragma mark initialization  -(void) initializecategoryarray {  sqlite3 *db= [keycryptappappdelegate getnewdbconnection]; keycryptappappdelegate *appdelegate = (keycryptappappdelegate *)[[uiapplication sharedapplication] delegate];  const char *sql = [[nsstring stringwithformat:(@"select category categories;")]cstring];   sqlite3_stmt *compiledstatement;    if (sqlite3_prepare_v2(db, sql, -1, &compiledstatement, null)==sqlite_ok) {     while(sqlite3_step(compiledstatement) == sqlite_row)         [categoryarray addobject:[nsstring stringwithutf8string:(char*) sqlite3_column_text(compiledstatement, 0)]];  } else {     nsassert1(0,@"error preparing statement", sqlite3_errmsg(db)); } sqlite3_finalize(compiledstatement); } /* - (id)initwithstyle:(uitableviewstyle)style { // override initwithstyle: if create controller programmatically , want perform customization not appropriate viewdidload. if ((self = [super initwithstyle:style])) { } return self; } */ #pragma mark - #pragma mark view lifecycle - (void)viewdidload { self.title = nslocalizedstring(@"delete categories",@"delete categories"); categoryarray = [[nsmutablearray alloc]init]; [self initializecategoryarray]; [super viewdidload];  // uncomment following line display edit button in navigation bar view controller. self.navigationitem.rightbarbuttonitem = self.editbuttonitem; } /* - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; } */ /* - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } */ /* - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; } */ /* - (void)viewdiddisappear:(bool)animated { [super viewdiddisappear:animated]; } */ /* // override allow orientations other default portrait orientation. - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations return (interfaceorientation == uiinterfaceorientationportrait); } */   #pragma mark - #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.categoryarray count]; }   // customize appearance of table view cells. - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {  static nsstring *cellidentifier = @"cell";  uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) {     cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; }  // configure cell... nsuinteger row = [indexpath row]; cell.text = [categoryarray objectatindex:row]; 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; } */ // override support editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {  if (editingstyle == uitableviewcelleditingstyledelete) {     // delete row data source      [categoryarray removeobjectatindex:indexpath.row];     [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:yes];     [deletecategorytable reloaddata];      nsstring *selectedcategory = [categoryarray objectatindex:indexpath.row];      sqlite3 *db= [keycryptappappdelegate getnewdbconnection];     keycryptappappdelegate *appdelegate = (keycryptappappdelegate *)[[uiapplication sharedapplication] delegate];     const char *sql = [[nsstring stringwithformat:@"delete categories category = '%@';", selectedcategory]cstring];      sqlite3_stmt *compiledstatement;       if (sqlite3_prepare_v2(db, sql, -1, &compiledstatement, null)==sqlite_ok)     {         sqlite3_exec(db,sql,null,null,null);      }     else {         nsassert1(0,@"error preparing statement", sqlite3_errmsg(db));     }     sqlite3_finalize(compiledstatement); }    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 - #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];  [detailviewcontroller release];  */ } #pragma mark - #pragma mark memory management  - (void)didreceivememorywarning { // releases view if doesn't have superview. [super didreceivememorywarning];  // relinquish ownership cached data, images, etc aren't in use. }  - (void)viewdidunload { // relinquish ownership of can recreated in viewdidload or on demand. // example: self.myoutlet = nil; }   - (void)dealloc { [super dealloc]; }   @end 

1) problem deleting is

[categoryarray removeobjectatindex:indexpath.row]; 

remove db too.

once delete row, reload tableview using [tableview reloaddata]

2) there no problem if 1 item

edit:

this problem

[categoryarray removeobjectatindex:indexpath.row]; nsstring *selectedcategory = [categoryarray objectatindex:indexpath.row]; 

you have deleted value @ indexpath.row array after accessing value @ same indexpath.row, delete next value only.. :)

so put

nsstring *selectedcategory = [categoryarray objectatindex:indexpath.row]

line before [categoryarray removeobjectatindex:indexpath.row];


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -