3

My UITableViewCells are getting a bit out of hand and I'm trying to structure them better. The issue is that the cells can have different layout structures such as number of labels, label widths and position, and can include different images and buttons. In order to perform the layout only once, I have created custom NIBs for each configuration, but this has resulted to over 20 different possible layouts which are hard to maintain and adjust.

I'm considering programmatic creation of the cells (avoiding storyboard prototypes and NIBs altogether) and was hoping to understand if this is a common practice and if there are performance tradeoffs I should be aware of.

What are your general suggestions in solving such an structure?

1
  • 2
    The main problem is that it is usually harder to "see" appearance through the code while it is naturally easy with nibs. To make it easier you'll have to write a really nice code and/or use convenient tools like Three20Style lib. Performance-wise you'll hardly notice a difference if both options are implement ed well. Commented Sep 11, 2013 at 18:24

3 Answers 3

1

Creating UITableViewCells through the code should neither give your advantages nor disadvantages compared to creating them through NIBs or by using multiple prototype cells. NIB files and prototype cells let you manipulate cell layout visually, but they do not prohibit you from additionally manipulating it in your program.

All three ways of building the cells have one common requirement - the cells of different kind must have different reuse identifiers (see this question for a discussion).

If it is the proliferation of the 20+ NIB files that you are worried about, consider the prototype cell approach, which lets you stay within a single storyboard file.

Sign up to request clarification or add additional context in comments.

2 Comments

The main issue I'm having with 20+ NIB files that I will also have with the storyboard setup, is when I want to change a common parameter between multiple cells, like the start position for a label that is used in a number of cells. If I do all of it programmatically it might be easier since I can use defined constants across the subclasses?
@x89a2s You can do the same thing if your layout is defined in NIBs/SB, too: for example, you could tag all labels that need to share an indent with the same tag, and then adjust the location of these labels programmatically, using constants that you define symbolically.
1

the way that I would solve that sort of problem is indeed switching to programmatic creation because then you can do subclass and implement and same functionality throughout your xibs only once. For instance lets say you have a cell that needs to have a square inside it colored red and sometimes you need another square colored green inside the red square. Well you can have a base class of MiddleSquareCell and then subclass it with GreenSquareCell which just has an override for painting the green square. Another advantage to programmatic solutions is configuration options. So for instance you could have a class SquareCell and then have a configuration option when you create it that says

[squareCell setRedSquareVisible]; 

Or

[squareCell setRedSquareVisible]; 
[squareCell setGreenSquareVisible]; 

Things like that. Anyways this is a way to implement it. I hope this helps.

Comments

0

If I understand what you are saying then I would definitely say that the programmatic route would be your best bet! You can design your custom cells in IB and link them up with their own class. In that class you can setup public properties that you can access so that you can set them programmatically.

Here is the typical cell code for a table view:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"code cell"];

    // from here you can have some if statements to determine the subclassing of that specific cell

    if ([cell isKindOfClass:[CustomCell class]]) {
        CustomCell *customCell = (CustomCell *)cell;

        customCell.label.text = @"Text";
        customCell.image = self.UIImageProperty;
    } else if (//Other kind of custom cell) {

    }

    return cell;
}

Doing it this allow's you to easily display multiple custom cells. Since you can subclass this will make your life much easier. Just remember that you need to set the class for each cell to whatever subclass you need.

The advantages to this are basic: Programmatically you have more flexibility to change your cell's and it will be more simple to subclass cell's than it is to create a bunch of nib files.

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.