Goal C: How to create custom / static table view cells through code

I am trying to create 3 cells of a table table using code (w / o nib). I'm having problems with the code. I guess I'm not getting this approach. Can someone advise me on the right way? Any help on this would be greatly appreciated!

Thanks!

Zhen hou

My code snippet is as follows:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 3; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; int row = [indexPath row]; UITableViewCell *startCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; UITableViewCell *durationCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; UITableViewCell *radiusCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; startCell.textLabel.text = @"Start:"; durationCell.textLabel.text = @"Duration:"; radiusCell.textLabel.text = @"radius"; if (row == 0) { return startCell; } else if (row == 1) { return durationCell; } return radiusCell; } 

EDIT (19/05/11)

After going through your answers, I still cannot display any cells in my table view. Is this due to how I initialized my table?

 //Initialization UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.settingsView.frame.size.height) style:UITableViewStyleGrouped]; self.tableView = tv; [self.view addSubview:tableView]; 

After that, I have an animation to extend the tableView

 [UIView animateWithDuration:0.3 animations:^{ [self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)]; }]; 

Do you guys see any problems with this? Anything that causes my cell display to fail?

Thanks!

ginseng

+4
source share
4 answers

In the UITableView initialization code, I do not see where you set the delegate and dataSource table dataSource . This is certainly part of the problem. According to Apple documentation :

A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically, these objects are either an application delegate or, more often, a custom UITableViewController object. The data source must accept the UITableViewDataSource protocol, and the delegate must accept the UITableViewDelegate protocol. The data source provides information that the UITableView must create tables and manage the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by the tables and performs other tasks, such as managing accessory types and selections.

This is what you can try:

 //Initialization UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.settingsView.frame.size.height) style:UITableViewStyleGrouped]; // assuming that your controller adopts the UITableViewDelegate and // UITableViewDataSource protocols, add the following 2 lines: tv.delegate = self; tv.dataSource = self; self.tableView = tv; [self.view addSubview:tableView]; 
+6
source

Do not create multiple cells at the same time and use the reuse mechanism with dequeueReusableCellWithIdentifier: You can customize cells (feed data from the model, etc.) based on their pointer path.

 - (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... switch(indexPath.row) { // assuming there is only one section case 0: cell.textLabel.text = @"Start:"; break; case 1: cell.textLabel.text = @"Duration:"; break; case 2: cell.textLabel.text = @"radius"; break; default: break; } return cell; } 
+2
source

The tableView: cellForRowAtIndexPath: called once per row in the table view. You do not need to create 3 cells each time. Create one cell for each call to this method and return it. Something like that -

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; int row = [indexPath row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } switch (row) { case 1:{ cell.textLabel.text = @"Start"; break; } case 2:{ cell.textLabel.text = @"Duration"; break; } default: cell.textLabel.text = @"radius"; break; } return cell; } 
+1
source

You must do self.view = self.tableView; . In a UITableViewController they are the same. And you installed the data source and delegated?

+1
source

All Articles