user2081064
user2081064

Reputation:

How do I have a UITableView with two different cell types and set each of the`cell layouts up programmatically, instead of via a Storyboard?

Previously I had this set up with a storyboard, having dragged the UILabels, positioned them and sized them whatnot on the UITableViewCell I dragged them onto, and then do a different version of that for the other UITableViewCell.

For example, like follows (but in the picture they've yet to be customized with the labels):

enter image description here

Then in the datasource, I'd simply check the Identifier, and depending on what the Identifier was, customize the cell accordingly.

However, I've needed more customization than I can get from the storyboard, as each cell is going to have two UIViews (a top one and a bottom one to allow sliding of the top one) so I can't really do this with storyboarding, as I add the labels and everything to the UIView programmatically.

But my question is: When I do it programmatically, how can I tell which cell is which so I can customize the layout of the UILabels accordingly? With a storyboard I can obviously just drag a UILabel onto each one, but when doing it programmatically and setting up the UIView, I don't know how to say, "Hey, if the identifier is this, add the UILabels like so" because the UIViews aren't aware of any Identifiers.

Basically the structure looks like this:

UITableView -> UITableViewCell -> CellFront(UIView) & CellBack(UIView)

And the look of the cell comes from the labels added to the CellFront UIView. But there's two looks to the cells and I don't know how to do it without a storyboard.

Upvotes: 2

Views: 2555

Answers (3)

rmaddy
rmaddy

Reputation: 318774

Here is one approach:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Adjust the condition to match your needs
    if (indexPath.row == 0) {
        static NSString *Identifier1 = @"CellType1";
        // cell type 1
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier1];

            // add subviews here
        }

        // set cell properties

        return cell;
    } else {
        static NSString *Identifier1 = @"CellType2";
        // cell type 2
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier2];

            // add subviews here
        }

        // set cell properties

        return cell;
    }
}

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

If you are creating the cells solely in code, then you register your UITableViewCell subclass in the viewDidLoad method of your table view controller. That method sets the identifier. Then, you use that identifier in cellForRowAtIndexPath: just like you would for a xib or storyboard created cell.

[self.tableView registerClass:[MyCellSubclass class] forCellReuseIdentifier:@"MyIdentifier"];

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Although UIViews are not aware of identifiers, they have a property called tag which can be used for any purpose that you would like. You can set the tag to, say, 1 on cells of one kind, and to 2 on cells of the other kind, and then use the tag to distinguish the cells in code. Moreover, once your views are tagged, you can call viewWithTag: on the containing view, and get back the view with the tag that you want.

Upvotes: 0

Related Questions