Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using it in UITableViewCell #23

Closed
danglade opened this issue Sep 27, 2015 · 11 comments
Closed

Using it in UITableViewCell #23

danglade opened this issue Sep 27, 2015 · 11 comments
Labels

Comments

@danglade
Copy link

Hello,

Love your project. Can I use this category in a UITableViewCell?

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

Yes, you can use it in any UIView, just put your layout code in layoutSubviews.

@danglade
Copy link
Author

Thank you! Will try it!

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

No problem, LMK if you have issues!

@mamaral mamaral closed this as completed Sep 27, 2015
@danglade
Copy link
Author

Maybe I'm doing something wrong when reusing the cells, but I don't think so. Look at the behavior of the cells.

Look at this video -> https://www.dropbox.com/s/uc9jqdpmvofom4v/Untitled.mov?dl=0

Let me know,

Thanks

#import "TableViewController.h"
#import "Cell.h"
#import "UIView+Facade.h"

@interface TableViewController ()
{
    UILabel *_label;;

}

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    [self layoutFacade];
}

- (void)layoutFacade {

    [_label anchorTopRightWithRightPadding:10 topPadding:10 width:110 height:20];

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
    return 200;

    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *cellIdentifier = @"cell";
    Cell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

        cell.userInteractionEnabled = YES;
        _label = [UILabel new];
        [_label sizeToFit];
        _label.font = [UIFont systemFontOfSize:24];
        _label.text = @"Pruebaaa";
        [cell addSubview:_label];

        // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.backgroundColor = [UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1] /*#f4f4f4*/;


                return cell;
}

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

I'm reading this on a phone at the moment so not 100% sure, but it looks like your adding 1 instance of a label as a subview to multiple cells.

@danglade
Copy link
Author

Correct, and it won't show that label until I scroll the view. Which is weird.

If I do cell.textlabel.text it will show at the beginning with no scrolling needed.

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

Yeah the problem has nothing to do with Facade or the layout, it has to do with cell reuse. You cant have the same view as a subview of multiple views. Each cell needs its own instance of that label.

@danglade
Copy link
Author

I understand, what would be the best practice here to use Facade on a UITableViewCell?

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

Subclass UITableViewCell, put a label in there, and in that cell's layoutSubviews method is where you would have your Facade code.

@mamaral
Copy link
Owner

mamaral commented Sep 27, 2015

In one of my open source apps, I have a UICollectionViewCell I do this with. You can reference the source here:

https://github.com/mamaral/xkcd-Open-Source/blob/master/Source/Views/Comic%20Cell/ComicCell.m

@danglade
Copy link
Author

I will check it out!

Thank you man!

Sent from my iPhone

On Sep 27, 2015, at 1:36 PM, Mike Amaral notifications@github.com wrote:

In one of my open source apps, I have a UICollectionViewCell I do this with. You can reference the source here:

https://github.com/mamaral/xkcd-Open-Source/blob/master/Source/Views/Comic%20Cell/ComicCell.m


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants