Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting static cells in uitableview programmatically

I am programmatically creating a tableview in objective c. How can I make the cells static programmatically?

Thanks

like image 764
jpo Avatar asked Jun 25 '12 15:06

jpo


3 Answers

Making cells static programmatically doesn't really make sense. Static cells are basically only for Interface Builder and requires the entire TableView to be static. They allow you to drag UILables, UITextFields, UIImageViews, etc. right into cells and have it show up just how it looks in Xcode when the app is run.

However, your cells can be "static" programmatically by not using an outside data source and hardcoding everything, which is usually going to be kind of messy and generally a poor idea.

I suggest making a new UITableViewController with a .xib and customizing it from there if you want "static" cells. Otherwise, just hardcode all your values and it's basically the same thing, but is probably poor design if it can be avoided.

like image 143
MikeS Avatar answered Sep 21 '22 15:09

MikeS


By using a distinct cell identifier for each one you will get it. You could use something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"s%i-r%i", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        //you can customize your cell here because it will be used just for one row.
    }

    return cell;
}
like image 25
Ricard Pérez del Campo Avatar answered Sep 22 '22 15:09

Ricard Pérez del Campo


You could also do it the old fashioned and just create the cell the way you want depending on the NSIndexPath, this works with Static Cell TVC's and regular table views (don't forget to return the proper number of sections and rows in their datasource methods):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch indexPath.row {
        case 0:
            // First cell, setup the way you want

        case 1:
            // Second cell, setup the way you want
    }

    // return the customized cell
    return cell;
}
like image 28
LJ Wilson Avatar answered Sep 22 '22 15:09

LJ Wilson