0

I have a UITableView with a custom cell and its corresponding .xib. My CustomCell.h has 3 properties. I want to be able to specify the width of these properties programmatically without using auto layout. The three properties are a background image, a header view, and a title within the header view. I want to set the with for all three properties to be the same as the cell.contentView width.

I've place [cell setNeedsLayout] in my cellForRowAtIndexPath but the only property that gets updated is the ImageView and the titleView in CustomCellOne. The with of the label does not get updated. An issue I have with this "solution", aside from the fact that it doesn't update the label width, is that when the cell is displayed I can see the background image width updating. The background image starts at 320 width and expands to 375 width. I want the layoutSubviews to be performed before the cell is displayed.

I've also tried placing [cell setNeedsLayout] inside willDisplayCell but it produces the same behavior mentioned above.

I've also tried [self setNeedsLayout] in the awakeFromNib method from CustomCellOne.m but that also produces the same result mentioned above.

This custom cell needs to be able to take into account the dimensions of the different iPhone sizes and [ImageManager] returns the appropriate background image for the specific device.

If I select the cell then the layoutSubviews will update as I want them to be. Is there a way to trigger programmatically the selection of a cell? This would not be the preferable solution since it's a hack and I'm sure there's a better way, the right way, to do what I want.

I will be forever in the debt of whoever can help me out with this. My code below.

// My ListViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.bounds = [self.view frame];
    self.view.backgroundColor = [UIColor colorWithRed:0.882f green:0.902f blue:0.922f alpha:1.00f];

    [self setNeedsStatusBarAppearanceUpdate];

    // Setup the table view
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    // Setup the bar
    self.myCustomBar = [[StyleOneBar alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), 100.0)];

    SquareCashStyleBehaviorDefiner *behaviorDefiner = [[SquareCashStyleBehaviorDefiner alloc] init];
    behaviorDefiner.elasticMaximumHeightAtTop = YES;
    [behaviorDefiner addSnappingPositionProgress:0.0 forProgressRangeStart:0.0 end:0.5];
    [behaviorDefiner addSnappingPositionProgress:1.0 forProgressRangeStart:0.5 end:1.0];
    behaviorDefiner.snappingEnabled = YES;
    self.myCustomBar.behaviorDefiner = behaviorDefiner;
    [self.view addSubview:self.myCustomBar];

    // Configure a separate UITableViewDelegate and UIScrollViewDelegate (optional)
    self.delegateSplitter = [[BLKDelegateSplitter alloc] initWithFirstDelegate:behaviorDefiner secondDelegate:self];
    self.tableView.delegate = (id<UITableViewDelegate>)self.delegateSplitter;

    //[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.tableView.contentInset = UIEdgeInsetsMake(self.myCustomBar.maximumBarHeight, 0.0, 0.0, 0.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CustomCellOne *cell = (CustomCellOne *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"CustomCellOne" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    }

    NSString *imageName = [NSString stringWithFormat:@"sunbathing%@", [ImageManager getCollectionImagePartialName]];
    [cell.ivCategory setImage:[UIImage imageNamed:imageName]];
    [cell.lblTitle setText:@"Lazy Beach Day"];
    cell.titleView.frame = CGRectMake(0, 0, CGRectGetWidth(cell.contentView.bounds), 29);

    return cell;
}

My Custom Cell .h file

// CustomCellOne.h
#import <UIKit/UIKit.h>

@interface EventCellOne : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *ivCategory;
@property (retain, nonatomic) IBOutlet UIView *titleView;
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;


@end

My Custom Cell .m file

#import "CustomCellOne.h"
#import "ImageManager.h"

@implementation EventCellOne

- (void)awakeFromNib
{
    // Initialization code
    [super awakeFromNib];

    self.contentView.backgroundColor = [UIColor clear];
    [self setNeedsLayout];
}

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

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //self.bounds = self.contentView.bounds;

    [ImageManager getCollectionImageHeight]);
    CGRect imageViewBounds = CGRectMake(0, 0, [ImageManager getImageMaxWidth], [ImageManager getCollectionImageHeight]);
    [self.ivCategory setFrame:imageViewBounds];

    self.titleView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), 29);
self.titleView.backgroundColor = [UIColor redColor];

    self.lblTitle.frame = CGRectMake(3, 4, CGRectGetWidth(self.contentView.bounds)-6, 21);
    [self.lblTitle setBackgroundColor:[UIColor lightGrayColor]];

}
titan
  • 524
  • 1
  • 6
  • 19
WillJ
  • 1
  • 1
  • Correct me if I'm wrong, but layoutSubviews doesn't get called when AutoLayout is disabled. And you have to do any drawing in drawrect. And make sure you're setting autoTranslateIntoContraints to false. Also take a look at how the draw cycle works. – I make my mark Oct 05 '15 at 10:57
  • @user3654258 - funny thing is that layoutSubviews in CustomCellOne.m does get called but none of the property frame modifications in that method take effect. I change the with of the UIImageView but nothing happens. The width remains at whatever is set in the .xib for that property. I placed an NSLog inside layoutsubviews and it gets called for every cell that's being displayed. – WillJ Oct 05 '15 at 21:03
  • @user3654258 I've added the drawing to drawRect. The UIImageView and UILabel widths does get modified but the UILabel remains unchanged. Regardless of whatever width I change it to, it keeps the width specified in the .xib for that IBOutlet – WillJ Oct 05 '15 at 21:11

1 Answers1

0

Found the solution. I had to uncheck "Use Auto Layout" in IB for the custom cell .xib. My Custom Cell .m file only has layoutSubviews. No need for the drawrect method.

Thank you to @user3654258 with his assistance.

WillJ
  • 1
  • 1