0

I am trying to override the setFrame method of UITableViewCell inside of a class that inherits from UITableViewController. I found this method override as an answer to this question but I don't know how to implement the override to get it to work.

Here is the override I want to implement:

- (void)setFrame:(CGRect)frame {
    int inset = 1;
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}

This is the class I want to use the override in:

@interface PeopleTableViewController : UITableViewController 
{
}

@end

The previous answer says to subclass UITableViewCell to override the method. How and where do I do this? Thanks in advance

EDIT: This is where the UITableViewCell is used.

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

    // Configure the cell...
    //USE TO SET CELL IMAAGE BACKGROUND

    cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"basketball.png"] 
                            stretchableImageWithLeftCapWidth:0.0 
                                                topCapHeight:5.0]];

    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"basketball.png"] 
                                    stretchableImageWithLeftCapWidth:0.0 
                                                        topCapHeight:5.0]];

    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];


    return cell;
}
Community
  • 1
  • 1
JaaMills
  • 11
  • 1
  • 3

2 Answers2

0

You should do it in a UITableViewCell subclass, this is the easiest and safest option.

If you want to do it in the view controller for some reason (you risk breaking a number of things) you would need to use method swizzling (because you want to call super so using a category won't work).

Wain
  • 118,658
  • 15
  • 128
  • 151
0

Your main issue here is that you are looking at your UITableViewController subclass. If you subclass UITableViewCell you will get some default method implementations. Just add your setFrame override in the implementation somewhere, like this:

#import "MyTableViewCellSubclass.h"

@implementation MyTableViewCellSubclass

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self; 
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state 
}

// YOUR ADDED setFrame OVERRIDE
- (void)setFrame:(CGRect)frame {
    int inset = 1;
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}

@end

Just to give you something to think about. UIViewControllers do not have frames. They just control views (hence "viewController"). Views have frames. Hope that helps you understand why we put the setFrame override in a view class and not a controller class.

Firo
  • 15,448
  • 3
  • 54
  • 74
  • So if I make a subclass of UITableViewCell called MyTableViewCell and replace UITableViewCell *cell with MyTableViewCell, will all of the other automatically inserted code that was already there work? I will post the method I am talking about – JaaMills Jul 30 '13 at 16:19
  • It should, yes. If you do not override a method in a subclass it will just use the super class's by default. So in this case if you do not override anything else, it will act just like a `UITableViewCell` (with the exception of setting the frame because you overrode it) – Firo Jul 30 '13 at 16:22
  • Thank You! Worked just as you said. Sorry if that was a rookie question, thought it would be difficult since the first thing I looked up mentioned multiple inheritance. Appreciate the fast response :] – JaaMills Jul 30 '13 at 16:33
  • No problem. We were "rookies" at one point and in some area everyone still is. Just glad I could help! – Firo Jul 30 '13 at 16:37