4

I am trying to create 3 table view cells using code (w/o nib). I am having some trouble getting the code to work. I guess I am not getting the approach right. Can anyone advise me on the proper way going forward? Any help on this will be greatly appreciated!

Thanks!

Zhen Hoe

My code snippet 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 am still unable to display any cells in my tableview. Is this due to the way 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 which I have an animation to expand 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 issues with the above? Anything that is causing my display of cells fail?

Thanks!

Zhen

3
  • While not exactly the right way, this should work. Can you tell how it is failing? Commented May 18, 2011 at 19:06
  • Hi, I am not getting any displays on my tableview. I have included my initialization code. Do you see any issues? Thanks! Commented May 18, 2011 at 19:19
  • Have you set the datasource? Commented May 18, 2011 at 19:23

4 Answers 4

6

In your UITableView initialization code, I don't see where you set the table view delegate and dataSource. That is certainly part of the problem. As per Apple's 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 the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.

This is what you can try to do:

//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];
Sign up to request clarification or add additional context in comments.

Comments

2

Do not create more than one cell at a time and make use of the reuse mechanism via dequeueReusableCellWithIdentifier:. You can configure the cells (feed data from model, etc.) based on their index 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;
}

1 Comment

Hi, thanks for the response. I have edited my question to include the way I initialized the table view. Not sure if there are any issues there. Great if you can give me advise on this. Thanks!
1

The tableView: cellForRowAtIndexPath: method is called once per row in your table view. You don't need to create 3 cells each time. Create one cell for each invocation of this method, and return it. Something like this -

- (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;
}

2 Comments

Hi, I followed your code but am still not able to get the display. I have updated my question to include the way I initialized and animate the table View. Do you see any issues there?
Have you tried what some others (@octy and @Deepak) suggested? You're probably not setting the delegate / datasource. Did you put a breakpoint in this code to see if it is hit?
1

You should do self.view = self.tableView;. In a UITableViewController, they are the same. And did you set the datasource and delegate?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.