oe a
oe a

Reputation: 2280

Adding static content into UITableViewCell

I am using a UIViewController instead of a UITableViewcontroller and am having trouble understand the concept of adding static content into a table. I've programmatically done dynamic content before but never static content.

Here is what I have designed in IB:

enter image description here

All of this content is inside of a "Content View' inside of a TableCell.

I've set my datasource and delegates, I only have one section and one cell returned. Here is my code for the cells. Typically I would use an array to populate this but I have no idea how to do so with static content in IB. How do I add the objects from the picture, most importantly how do I preserve the location of each object? I hope I don't have to type in manual x, y coordinates.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }


    return cell;
}

EDIT: Static cells works fine in a UITableViewController....is there any way to make it work with the UIViewController or will I have to switch it all over?

Upvotes: 0

Views: 307

Answers (2)

MiQUEL
MiQUEL

Reputation: 3001

You have to use UITableViewController for static cells.

This is my workaround usign UIViewController.

- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }
    NSString * titol = [NSString new];
    NSString * valor = [NSString new];
    NSDictionary * el_contacte = [contactes objectAtIndex:(indexPath.row)];

    switch (indexPath.row) {
        case 0:
            titol = @"Nombre";
            valor = [el_contacte valueForKey:@"nombre_contacto"];
            break;
        case 1:
            titol = @"Teléfono";
            valor = [el_contacte valueForKey:@"telefono"];
            break;
        case 2:
            titol = @"Extensión";
            valor = [el_contacte valueForKey:@"extension"];
            break;
        case 3:
            titol = @"Móvil";
            valor = [el_contacte valueForKey:@"movil"];
            break;
        case 4:
            titol = @"Fax";
            valor = [el_contacte valueForKey:@"fax"];
            break;
        case 5:
            titol = @"email";
            valor = [el_contacte valueForKey:@"email"];
            break;

        default:
            break;
    }
    cell.textLabel.text = titol;
    cell.detailTextLabel.text = valor;

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString * retornar = [NSString new];
    switch (section) {
        case 0:
            retornar = @"Datos del cliente";
            break;
        case 1:
            retornar = @"Contacto";
            break;
        case 3:
            retornar = @"Personas";
            break;
        default:
            break;
    }
    return retornar;
}

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

Yes, you can do it in a UIViewController, but you would still be using dynamic cells, but in a static way. By that, I mean you set up all your cell's subviews in the storyboard, putting any text you want in your labels, and images in any image views, etc. Then in cellForRowAtIndexPath, you just dequeue the cell and return it without doing anything else. If you have multiple cells with different content, then you need to create different dynamic prototypes, each with its own identifier. In cellForRowAtIndexPath use an if-else block to test which indexPathRow is passed in, and dequeue the correct cell type that you want at that location.

Upvotes: 0

Related Questions