UITableViewCell: imageView resize in edit mode - ios

I subclassed UITableViewCell (subtitle style) and I have to set the imageView property runtime. The size of the image can be different for each cell, but I want the imageView to be squared (fixed size). For this reason I set at first a squared image as a placeholder. This is the code of the init method:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
// Other settings...
[self.imageView.layer setCornerRadius:2.0f];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.imageView setClipsToBounds:YES];
self.imageView.image = [UIImage imageNamed:#"placeholder"];
return self;
}
In the layoutSubviews method I just call the super method, while I set the image in the UITableViewController once I get it from its URL. This is working fine as you can see here:
But when I enter in edit mode something happens and I get this:
I tried to set imageView frame in layoutSubviews method but the image is not aligned and textDetail label keep starting in the same position of the above screenshot... where am I wrong?

Related

Create Custom UITableViewCell Programmatically - Objective C

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
//[self createViews];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"draw rect");
[self createViews];
}
I'm creating a custom UITableViewCell. I require creating a UILabel that depends on the height of the UITableViewCell, and the height is not yet set in initWithStyle (it returns the default 44 when in reality the height of my cell varies greatly). For this reason, I call my createViews function in drawRect. This was working well, however I'm noticing that the function can be called again when I insert and delete rows.
My Question:
Does it make sense to call my createViews function inside drawRect?
You have few options here.
1. Use layoutSubviews/awakeFromNib, check whether the subviews were created, if no, create them with correct frames.
2. Use init to create views with:
Constraints
Without constraints and in layoutSubviews/awakeFromNib try to change the frame

UIImageView not redrawing

I'm using code to resize a UIImageView by reducing its width to .5 its original value.
The code is
- (void)viewDidLoad
{
[super viewDidLoad];
self.allisonHall.newWidth = .5;
self.allisonHall.frame = CGRectMake(self.allisonHall.frame.origin.x,
self.allisonHall.frame.origin.y,
self.allisonHall.frame.size.width*self.allisonHall.newWidth,
self.allisonHall.frame.size.height);
self.allisonHall.contentMode = UIViewContentModeLeft;
self.allisonHall.clipsToBounds = YES;
NSLog(#"%f",self.allisonHall.frame.size.width);
}
and the result of the NSLog is 140, .5*280 as expected.
However, in the view the UIImageView remains at its original width.
The UIImageView is linked as
#property (weak, nonatomic) IBOutlet XYZdiningHall *allisonHall;
within the view controller's .h file, where XYZdiningHall is a subclass of UIImageView
Should the UIImageView's frame be changing given this code or do I need to call another method to fire the change?
Do you need to use UIViewContentModeLeft? You can use UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill and the image will be redrawn (the aspect modes are related to the image view bounds, Left isn't).

Trouble Adding Corner Radius to ImageView Inside TableViewCell

I've created a custom UITableViewCell class where I've created a label and imageview, and connected them via storyboard. What's the best place to apply corner radius, border width, etc. to the imageView?
I tried the following in the custom class' init method but it didn't quite work:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
productImageView.layer.borderColor = (__bridge CGColorRef)([UIColor whiteColor]);
productImageView.layer.borderWidth = 3.0f;
productImageView.layer.cornerRadius = 36.0f;
}
return self;
}
What's wrong here?
Are you doing in this in the UITableViewCell subclass? If so you need add this in awakeFromNib or prepareForReuse. When init is called the image view doesn't exist yet. Also do self.productImageView.clipsToBounds = YES;

How to add multiple buttons in accessory view of UITableView?

I want to add two adjacent custom buttons in the UITableView's accessory view.
I tried doing cell.accessoryView = customButton; and then
cell.accessoryView = customButton2 .It is obvious that this replaces the previous button.
Thanks for any help!
You can add a UIView containing the two buttons as a custom accessoryView.
UIView *buttonsView = [...];
// add buttons to buttonsView
cell.accessoryView = buttonsView;
Or you can subclass UITableViewCell and add two buttons there.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIButton *buttonA = ....
UIButton *buttonB = ....
[self.contentView addSubview:buttonA];
[self.contentView addSubview:buttonB];
}
return self;
}
If you haven't done a custom UITableViewCell before this article might help.
http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702
You can make a custom UIView class with your buttons placed inside of it (as subviews: [self addSubview:button]). Then you can assign your custom UIView object as accessoryView of a cell.
cell.accessoryView = yourCustomView;

Update the preferredMaxLayoutWidth for a multiline UILabel with unknown size (Auto Layout)

I have a custom view class which inherits from UIView. This class has an UILabel as its subview. In the init-function of this custom view class I set up everything needed like this:
//h-file
#import <UIKit/UIKit.h>
#interface MyCustomView : UIView
#property (strong, nonatomic) UILabel *myLabel;
#end
//m-file
#implementation MyCustomView
#synthesize myLabel = _myLabel;
- (id)init
{
self = [super init];
if (self) {
_myLabel = [UILabel new];
if(_textView){
_myLabel.highlightedTextColor = [UIColor whiteColor];
_myLabel.translatesAutoresizingMaskIntoConstraints = NO;
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
_myLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_myLabel];
}
}
return self;
}
#end
I also set up a bunch of constraints to manage padding inside my custom view - furthermore there are constraints to layout multiple MyCustomView-instances for both vertical and horizontal axis as well.
To get a multilined label output I have to set the preferredMaxLayoutWidth-property of the UILabel myLabel. The width depends on the free space available. At http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html I read, that I can let Auto Layout calculate the width first and set it as preferredMaxLayoutWidth after the frame of the MyCustomView-instance (the label inside is single lined at this moment) has been set.
If I put the following function into the MyCustomView, the label still has a single line of text:
- (void)layoutSubviews
{
[super layoutSubviews];
float width = _myLabel.frame.size.width;
_myLabel.preferredMaxLayoutWidth = width;
[super layoutSubviews];
}
If I set the preferredMaxLayoutWidth to an explicit value inside the init-function, the label is multilined.
Does anybody know what I am doing wrong here?
Thanks in advance!
Without seeing all the constrains you have setup for your custom view, and the superview that contains it, it's really hard to determine the problem, I suggest you to print out all the view frames of the entire view hierarchy starting from the view controller's view at viewDidLayoutSubviews and determine if the label and its superviews have correct frame set.
I have an encountered similar issues with dynamic label size and scroll view so I created a prototype here, might be useful to you too: https://github.com/briandotnet/AutoLayoutScrollViewExperiment

Resources