Best way to handle UITableViewCell

I have a table that has about 10 cells, 4 different types. I subclassed UITextViewCell because I wanted to have an IBOutlet for the label and a UITextField. Not sure if this is the best way to handle this, but it works so far. Then I had a cell for the floor, so I decided instead of subclassing UITableViewCell, I took my already subclassed cell with UILabel and UITextField and wrote the following code:

NSArray *buttonNames = [NSArray arrayWithObjects:@"Male", @"Female", nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
segmentedControl.momentary = YES;               
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(75, 5, 130, 30);
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
for (UIView *oneView in cell.contentView.subviews) {
    if ([oneView isMemberOfClass:[UITextField class]]) {
        [cell.contentView insertSubview:segmentedControl aboveSubview:oneView];
        [oneView removeFromSuperview];
    }
}
[segmentedControl release];

      

How awful is it for that? Should I subclass UITableViewCell 5 times for one complex tableView? Does it work in extreme cases, such as in the following order?

+1


source to share


3 answers


I would go for subclassing. Subclasses are cheap. The cells are of different types, and what you are doing now is iterating over all subzones and checking every kind of class membership - that's slow! The subclass will clean up your code and make it faster at the same time. Don't try to put too many things in one container.



+2


source


You can always add the control to your subclass, but hide it. Then, depending on the row, set the cell "mode". The mode installer can hide / show controls related to this line. If performance is an issue, perhaps use multiple cell IDs to keep the views cached.



0


source


If this is the only table that will use these cells, I would go ahead and tweak them on the fly in the tableView: cellForRowAtIndexPath: method. Don't worry about subclassing.

And make sure you tag each subview (like tags). This way you can refer to the label using viewWithTag:

0


source







All Articles