Set image on tableView Cell size - ios

I have tableView Cell size and need to set image on tableView cell., but image not set of define the Cell size (following code into tableView Cell size stored cellSize variable).
My Code is...,
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
cell.imageView.image = [UIImage imageNamed:#"logo_black.png"];
above code in image not set image properly in tableView Cell.
So, How to set image on TableView Cell?

If you are trying to say that the image u are setting in each cell is not getting displayed at the right position then u can try doing the following:
first create a prototype cell and position the UIImageView at the right place in the cell u want and go to the scale properties of UIImage and note down the co-ordinates... then
instead of
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
this you can write the code as follows:
cell.imageView.frame = CGRectMake(x,y,width,height);
ex.
CGRectMake(0,0,20,20)

CGRect cellSize = cell.frame;
cell.imageView.frame = cellSize;

Try this code:
UIView *bgView = [[UIView allow ] init];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#"imgName"]];
cell.selectedBackgroundView = bgView;

Related

How to set an animation of images in background of table view cell

How do I programmatically set an image in the background of a table view cell? Can I animate it? Can I animate just a selected cell?
Note
I have used the Answer your own question feature to answer this question. Please, by all means add an answer if you've got other methods. :-)
To programatically add a background image to your table view cell (both custom and default) it is easiest done in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.
To set the background image, you need to create UIImageView reference, then assign that reference to an image. Then assign cell the UIImageView reference. This is done in the following code to have a default image throughout all cells in your tableview:
//have normal background image
//Create a UIImageView the size of your tableview row. My row (cell) size is 55 and I want it to expand the full length of the window (iPhone).
//You can create an icon look if you like, just play with the sizing.
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
//Next create a reference to the image
UIImage * regularImage = [UIImage imageNamed:#"Image"];
//assign the image to the UIImageView
cellBackgrounds.image = regularImage;
//set the ##background## of the cell
//cell has already been defined when you create the UITableViewCell * cell = ... code
[cell setBackgroundView: cellBackgrounds];
To animate the cell we do the same method except this time we refer to a set of images.
First we add a file to the project containing all the images we are going to display as an animation. To do this, simply drag the file into your Xcode project while Xcode is open and drop it in the left panel that contains all your .h and .m files. Make sure to name all your images in numerical order - image1, image2, image3, etc - before.
Then we use the following code. Notice the only change is the UIImage code:
//have animated background
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
//Change UIImage imageNamed to UIImage animatedImageNamed and refer to just the name of your images, not the number
//set the duration (which is in seconds) to whatever you like, testing to your desired flow
UIImage * animatedImages = [UIImage animatedImageNamed:#"image" duration:3.6];
cellBackgrounds.image = animatedImages;
[cell setBackgroundView: cellBackgrounds];
Now you have an animated table view cell background.
If you only want a selected row to be animated here's how you do it:
//Create a new reference to an index path
//I am referencing to the top cell in the first section
NSIndexPath *firstCell = [NSIndexPath indexPathForRow: 0 inSection:0];
//Create a new reference to the UITableViewCell
UITableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:firstCell];
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
UIImage * animatedImages = [UIImage animatedImageNamed:#"image" duration:3.6];
cellBackgrounds.image = animatedImages;
[cell setBackgroundView: cellBackgrounds];
This new code will prevent the text you're trying to display from displaying properly so add this to bottom of this block of code to ensure the text displays properly
topCell.title.text = your reference to Strings to be displayed
Enjoy.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//1. Setup the CATransform3D structure
CATransform3D rotation;
rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;
//2. Define the initial state (Before the animation)
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;
cell.layer.transform = rotation;
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//3. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:#"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];
}

unable to set the image view to be circle

i need to make the profile photo image circle (similar to whats app)
The cell i use is UITableViewCellStyleDefault
here's my code:
User *user = [DBHelper fetchUserWithUserID:userID];
cell.imageView.image = [Helper imageWithBlob:user.profile.thumbnailPhotoBlob defaultImageFile:FILE_DEFAULT_PHOTO];
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.clipsToBounds = YES;
cell.imageView.layer.masksToBounds = YES;
But the result image view is still a square
EDIT: The image view's frame is {0, 0, 0, 0}, but it did show the image.
EDIT 2:
I tried the answer but the image is not circle. Here's what i did:
float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
[Helper circlizeImageView:cell.imageView cellHeihgt:height];
+ (void)circlizeImageView:(UIImageView *)imageView cellHeihgt:(float)cellHeight {
imageView.frame = CGRectMake(0, 0, cellHeight, cellHeight);
[Helper circlizeView:imageView];
}
+ (void)circlizeView:(UIView *)view {
view.layer.cornerRadius = view.frame.size.height / 2;
view.clipsToBounds = YES;
view.layer.masksToBounds = YES;
}
The code is all fine.
The only problem is with this line:
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
//CGRect rectTest = cell.imageView.frame;
//returns a rect of (0,0,0,0) & hence the cornerRadius being set is 0.
Try:
cell.imageView.layer.cornerRadius = 10; //or some fixed value
In retrospect, what you're trying to do will not get you to make the imageView circular because the default imageView is rectangular (the height is always more than the width) and so... setting a corner radius that is half of the height/width will not work.
It'll be better if you use a custom UITableViewCell and add a square framed imageView.
ANyways... Since the default imageView touches the top of the default UITableViewCell and ends at the bottom, you can get the height of the imageView by using the height of the row (in case you have dynamically sized cells)
So, something as simple as this:
cell.imageView.layer.cornerRadius = cell.frame.size.height/2;
But none of this looks good since the imageView is rectangularish and cells being of dynamic height will make it look like a poor design (the fixed radius looks best imho)
You have two options here:
1- Add the imageView as a subView to you cell.
2- Override layoutSubviews in a custom cell:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,40,40);
}

Cell Content (Textview) only Update after touching the cell

i created a UITableViewCell with a UITextView inside.
I´m using Json to get my Html Content for the Textview. I´m using DTCoreText also. Here a part of my cellForRowAtIndexPath:
static NSString *CellIdentifier1 = #"NewsCell";
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(newsCell == nil){
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"GTNewsCustomCell"
owner:self//transfer ownership to self
options:nil];
newsCell = [nibViews objectAtIndex:0];
}
newsCell.cellFrame = CGRectMake(10, 0, 300,40);
newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:#"title"];
newsCell.messageTextView.textColor = [UIColor blackColor];
newsCell.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
newsCell.messageTextView.editable = NO;
newsCell.messageTextView.userInteractionEnabled = NO;
newsCell.messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *htmlTag = #"<b></b>";
NSString *html = [NSString stringWithFormat:#"%#%#",htmlTag,[[self.newsList objectAtIndex:indexPath.section]objectForKey:#"previewMessage"]];
.
.
.
return newsCell;
The Problem is that the Content of my TextView is sometimes bigger than the textview size. The strange thing is when i touch the textview, it updates himself and everybody is shown correct....what could be wrong?
Here are some Screenshots:
Broken TextView:
When i touch the Cell, the Text is formatted:
EDIT:
Ok no i have found my Problem but i dont know how to solve it. In my Custom Cell .m File i have a method for change the cell size width:
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect cellRect = self.bounds;
cellRect.size.width = self.cellFrame.size.width;
self.bounds = cellRect;
}
If i delete this everything works fine, BUT of course my cell have not the correct size anymore...but i need some space from the left and from the right between the TableView and the Cell!
Ok finally i solved the issue: I delete the "layoutSubviews" Method, and insert:
- (void)setFrame:(CGRect)frame {
frame.origin.x += 10;
frame.size.width -= 2 * 10;
[super setFrame:frame];
}
Now my Text always have the correct Size and my Cell have also the correct Size!
I always think it's best not to try to tamper with the frame/bounds of a UITableViewCell. Each cell's frame is set by the UITableView, with width equal to the table view's width, and height equal to either the value returned by the table view delegate's tableView:heightForRowAtIndexPath:, or if that's not implemented, the table view's rowHeight property.
If your custom cell's contents, i.e. subviews, are not where you want them, then position and size these in your custom layoutSubviews implementation (or in your nib). Auto layout can be an option here. In your example, you would lay out the cell's messageTextView.

UILabel have a weird grey top line/border in iOS7, how can I remove it?

I have a basic UILabel that I use in a UITableViewCell. I'm experiencing some strange behaviour, on some cells (not all) the UILabel gets a grey top border, see picture below. And I'm not sure how to fix it.
I create my UILabel like this:
if (self.postText == nil) {
CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
self.postText = [[UILabel alloc] initWithFrame:postTextRect];
self.postText.backgroundColor = [UIColor whiteColor];
self.postText.textColor = [UIColor darkGrayColor];
self.postText.userInteractionEnabled = NO;
self.postText.numberOfLines = 0;
[self.containerView addSubview:self.postText];
}
Any ideas on how to fix this?
Update
My cellForRowAtIndexPath looks like this:
- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[...]
static NSString *postCellIdentifier = #"PostCell";
PostCell *cell = [tableView dequeueReusableCellWithIdentifier:postCellIdentifier];
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:postCellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath forPost:cellPost];
return cell;
}
And the relevant par of configureCell like this:
- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
[...]
cell.username.text = cellUser.username;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = cell.postText.frame;
newFrame.size.height = expectedLabelSize.height;
cell.postText.frame = newFrame;
CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
if (containerRect.size.height < 65) {
containerRect.size.height = 65;
}
cell.containerView.frame = containerRect;
[...]
}
I had the same problem and solved it by rounding up the height:
newFrame.size.height = ceilf(expectedLabelSize.height);
Got the same issue
Solved by assigning border color and width to UILabel
nameLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
nameLabel.layer.borderWidth = 2.0f;
It must be a iOS's bug. The bug is triggered by the float height value of the label. So, the easy and no side-effect way to solve it is converting the float height value to int value:
(int)(label.frame.size.height)
Decided to use a UITextView instead.
The problem seems to occur when changing the UILabel's frame.
Chaning this code:
CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = cell.postText.frame;
newFrame.size.height = expectedLabelSize.height;
cell.postText.frame = newFrame;
to...
[cell.postText sizeToFit];
Did remove the border. But the content wasn't displayed just as I wanted it, that's why I changed to UITextView
The weird grey outlines is due to the background size and label size is not exact the same.
It can be solved perfectly by:
[myLabel sizeToFit]
The real reason is your frame height or width is not integer or change your app view` background color to clear color.
把frame的width 或者height修改为整数,或者设置背景色为clear color 即可.
try this:
cell.postText.layer.borderColor = self.Detail.backgroundColor.CGColor;
cell.postText.layer.borderWidth = 1;
It's seem that the problem appears when you change the frame property of the label, setting width or height with float value.
This solution works for me, setting the backgroundColor of the label to Clear.
Objective-C:
[label setBackgroundColor:[UIColor clearColor]];
swift:
label.setBackgroundColor = UIColor.clearColor()
I had the same issue. just doing labelView.layer.opaque = false; fixed mine. hope this will be useful to someone.
I found this problem in a .xib that was partially implemented with Auto Layout and partially implemented with Translates Mask Into Constraints. Once I converted it to full Auto Layout, the problem went away even though the sizes still had some half-points.

Setting the size for an image in a TableViewCell

I have a table with rows of height 90.
I want to have an image on each row of size 50x50.
I am doing the following in cellForRowAtIndexPath:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
if (item.image){
cell.imageView.image = [item.image smallImage];
cell.imageView.alpha = 1.0f;
} else {
cell.imageView.image = [UIImage imageNamed:#"icon-greyed.png"];
cell.imageView.alpha = 0.25f;
}
However the images appear with height 90 (the height of the row). What am I not doing right?
You will need to either add a UIImageView to cell.contentView which I wouldn't recommend, or create a custom UITableViewCell, which I would recommend. Then you can add a UIImageView to your custom cell and resize it to the dimensions you require.

Resources