iphone - UITableView count not increasing -
i have uitextview in viewcontroller. im passing textview viewcontroller1 uitableview. used nsuserdefaults store , retrieve text. in viewcontroller1 uitebleview not increasing updating text, replacing old text in first row.
viewcontroller:
-(void)save:(id)sender{ nsuserdefaults *userdata1 = [nsuserdefaults standarduserdefaults]; [userdata1 setobject:textview.text forkey:@"savetext"]; [userdata1 synchronize]; }
viewcontroller1:
-(void) viewwillappear:(bool)animated{ [super viewwillappear:animated]; textarray=[[nsmutablearray alloc]init]; txt=[[uitextview alloc]initwithframe:cgrectmake(0, 0, 320, 400)]; nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; // getting nsstring nsstring *savedvalue = [prefs stringforkey:@"savetext"]; txt.text = [nsstring stringwithformat:@"%@", savedvalue]; myappdelegate = (appdelegate *)[[uiapplication sharedapplication] delegate]; // [myappdelegate.textarray addobject:txt.text]; if(![myappdelegate.textarray containsobject:txt.text]){ [myappdelegate.textarray addobject:txt.text]; } nsuserdefaults *userdata1 = [nsuserdefaults standarduserdefaults]; [userdata1 setobject:myappdelegate.textarray forkey:@"save"]; [userdata1 synchronize]; [self.view addsubview:txt]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // nslog(@"textarray count %d",[myappdelegate.textarray count]); nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]]; return [mymutablearrayagain count]; } - (uitableviewcell *)tableview:(uitableview *)tableview1 cellforrowatindexpath:(nsindexpath *)indexpath { nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]]; static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview1 dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; } // configure cell... cell.textlabel.text = [mymutablearrayagain objectatindex:indexpath.row]; [cell.textlabel setfont:[uifont fontwithname:@"arial-boldmt" size:14]]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; return cell; }
declare array object in appdelegate.h
@property (nonatomic, retain) nsmutablearray *textarray;
allocate array object in appdelegate.m
in didfinishlaunchingwithoptions
method
nsmutablearray *array = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]]; if(array) { self.textarray = array; } else { self.textarray=[[nsmutablearray alloc]init]; }
import appdelegate.h
file in viewcontroller1.h
, declare this
appdelegate *appdelegate;
change following method in code in viewcontroller1.m
file
- (void)viewdidload { [super viewdidload]; tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 0, 320, 568) style:uitableviewstyleplain]; nslog(@"scrolling"); tableview.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblerightmargin; // tableview.contentinset = uiedgeinsetsmake(0, 0,300, 0); //values passed - top, left, bottom, right tableview.delegate = self; tableview.datasource = self; [tableview reloaddata]; tableview.contentinset = uiedgeinsetsmake(0, 0,300, 0); //self.view = tableview; [self.view addsubview:tableview]; } -(void) viewwillappear:(bool)animated{ [super viewwillappear:animated]; nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; // getting nsstring nsstring *savedvalue = [prefs stringforkey:@"savetext"]; txt.text = [nsstring stringwithformat:@"%@", savedvalue]; appdelegate = [[uiapplication sharedapplication] delegate]; if(![appdelegate.textarray containsobject:savedvalue]){ [appdelegate.textarray addobject:savedvalue]; } }
change following 2 methods
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [appdelegate.textarray count]; nslog(@"textarray count %d",[textarray count]); } - (uitableviewcell *)tableview:(uitableview *)tableview1 cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview1 dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; } // configure cell... cell.textlabel.text = [appdelegate.textarray objectatindex:indexpath.row]; [cell.textlabel setfont:[uifont fontwithname:@"arial-boldmt" size:14]]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; return cell; }
Comments
Post a Comment