ios - Creating a UITableView Programmatically -


i have application in xcode 4.6 uses storyboards. added uitableview view controller class, worked expected. however, when tried deleting uitableview in storyboard , adding same class programmatically, ran 2 specific problems:

1) although set uitableviewcell type of type subtitle, detail label no longer appears.

2) segue should occur when select cell not occur, , prepare segue not being called, indicating no message being sent table view when cell selected.

here relevant code:

@interface statstableviewcontroller () <uitableviewdatasource, uitableviewdelegate> @property (strong, nonatomic) uitableview *tableview;  @end  @implementation statstableviewcontroller  -(uitableview *)maketableview {     cgfloat x = 0;     cgfloat y = 50;     cgfloat width = self.view.frame.size.width;     cgfloat height = self.view.frame.size.height - 50;     cgrect tableframe = cgrectmake(x, y, width, height);      uitableview *tableview = [[uitableview alloc]initwithframe:tableframe style:uitableviewstyleplain];      tableview.rowheight = 45;     tableview.sectionfooterheight = 22;     tableview.sectionheaderheight = 22;     tableview.scrollenabled = yes;     tableview.showsverticalscrollindicator = yes;     tableview.userinteractionenabled = yes;     tableview.bounces = yes;      tableview.delegate = self;     tableview.datasource = self;      return tableview; }  - (void)viewdidload {     [super viewdidload];     self.tableview = [self maketableview];     [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:@"newfriendcell"];     [self.view addsubview:self.tableview]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"newfriendcell";     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];      if (cell == nil)      {         cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];     }      friend *friend = [self.fetchedresultscontroller objectatindexpath:indexpath];       **//this data appears**     cell.textlabel.text = friend.name;     cell.textlabel.font = [cell.textlabel.font fontwithsize:20];     cell.imageview.image = [uiimage imagenamed:@"icon57x57"];      **//this data not appear**     cell.detailtextlabel.text = [nsstring stringwithformat:@"%i games", friend.gamecount];     cell.detailtextlabel.textcolor = [uicolor lightgraycolor];      return cell; }   -(void)tableview:(uitableview *)tableview diddeselectrowatindexpath:(nsindexpath *)indexpath {     [self performseguewithidentifier:@"detailsview" sender:self]; }  - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {       //i set segue identifier in interface builder      if ([segue.identifier isequaltostring:@"detailsview"])      {           nslog(@"segue"); //check see if method called, not called upon cell touch           nsindexpath *indexpath = [self.tableview indexpathforcell:sender];          ///more code prepare next view controller....      } } 

i not sure forgetting can solve these 2 issues. appreciated.

when register class, , use dequeuereusablecellwithidentifier:forindexpath:, dequeue method guaranteed return cell, if (cell == nil) clause never entered. so, old way, don't register class, , use dequeuereusablecellwithidentifier:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"newfriendcell";     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];      if (cell == nil) {         cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];     } //etc. return cell; } 

as segue, can't called because can't make segue table you've created in code, not in ib. again, go old way , use tableview:didselectrowatindexpath: called when select cell. instantiate detail controller there , trasition in code.

after edit:

i didn't see added code there. you've implemented diddeselectrowatindexpath rather didselectrowatindexpath. if change that, segue should work.


Comments