Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: How to create custom/static table view cells via code

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

like image 452
Zhen Avatar asked Dec 27 '22 20:12

Zhen


2 Answers

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];
like image 68
octy Avatar answered May 22 '23 05:05

octy


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;
}
like image 44
albertamg Avatar answered May 22 '23 03:05

albertamg