In my custom cell I want to center an image but this does not work.
self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ;
float h=cell.frame.size.height;
float w=cell.frame.size.width;
CGRect r = leftImage.frame;
r.origin = cell.frame.origin;
r.origin.x = w/ 2 - r.size.width / 2;
r.origin.y = h / 2 - r.size.height / 2 + 12;
leftImage.frame = r;
[self.contentView addSubview:leftImage];
I think You tried to do this in - (UITableViewCell *)tableView:(UITableView *)tableViewRef cellForRowAtIndexPath:(NSIndexPath *)indexPath when creating new cell or in cell itself in init section. At this moments cell don't have correct frame and you can't rely on it.
Try setting the leftImage's autoresizingMask:
self.leftImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin
When the cell is created, you're never sure what its dimensions are. Mostly, a cell will be initialized with a CGRectZero frame and later on, the UITableView will resize the cell appropriately.
Related
I am trying to create a horizontal scroll view which has a UILabel as elements, by doing the following in viewDidLoad:
for(int index=0; index < [self.category count]; index++)
{
UILabel *label = [[UILabel alloc] init];
label.text = [self.category objectAtIndex:index];
label.textColor = [UIColor blackColor];
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(5+xOffset, 0, expectedLabelSize.width, 40);
self.scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[self.scrollView addSubview:label];
xOffset += 170;
}
However, I can't see anything when I run the app in the simulator, what am I missing here? Pretty sure that the UIScrollView is connected via the IBOutlet and I know that the text exists as I tried printing that out via NSLog
UPDATE:
Also how do I check which UILabel is clicked? I wanted to know this as well..
I think you did'nt initialize xOffset=0 before running for loop. so that it is taking a garbage value and then executing xOffset += 170; instruction.
so please initialize xOffset=0;
If you're positioning the labels with a frame you should also set autoresizingMask to make sure if you position based on a portrait layout but are testing the app in landscape, your labels don't get auto-positioned outside the new bounds. Also, make sure to autorelease those labels or else you'll be leaking memory.
Your expectedLabelSize calculation seems to adjust the height, leaving the width at 100. When you set the frame of your UILabel you are just using this width.
I'm trying to center a UIImageView width-wise in a UITableViewCell with the following code:
// self.cover_image is a UIImage variable
// cell is a UITableCellView
UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image] autorelease];
[backcover_imageview sizeToFit];
//center image
//float xPos = cell.contentView.frame.size.width - (self.cover_image.size.width/2);
//TODO: need better solution
//Eyeballing attempt:
float xPos = self.cover_image.size.width - 25;
CGRect bookcover_frame = backcover_imageview.frame;
bookcover_frame.origin.x = xPos;
backcover_imageview.frame = bookcover_frame;
backcover_imageview.tag = 9000;
backcover_imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[cell.contentView addSubview:backcover_imageview];
I'm trying to center the UIImageView in a UITableCellView regardless of what orientation the iPad or iPhone device is in. Does anyone know the right way to do this?
I added two more autoresizing masks and got a good result using your code and PengOne's answer:
playImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:playImage];
playImage.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
After you add backcover_imageView to the contentView of the cell, use this to center it:
imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
Can you try assigning the center property of cell to that of image view?
Something like this
imageView.center = cell.center;
And then add it to the cell.
You can use center anchors of the cell to keep the image in the center:
image.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
I have created a custom view cell with a UILabel in it, I have set :
cell.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.titleLabel.numberOfLines = 0;
and it's not going in multiple lines, why is this?
in the layoutSubviews I have:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = titleLabel.frame;
frame.origin.y = 5;
titleLabel.frame = frame;
}
and that's about all the settings I have, however for a long text put in the label in the cell it just won't word wrap:
What is the height of the label's frame? It needs to be tall enough to hold more than one line of text. Check out Adjust UILabel height depending on the text
I have a requirement to fill cells with varying length wraparound text. My current routine is handling this satisfactorily, however, I am concerned about the use of a couple of constants being used.
The values 10 and 260 below represent the margin and cell width expected, but they are only accurate for standard definition resolution in portrait orientation.
Is there some screen/table object that provides me these values as metrics I could use instead of the constants?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
UILabel *lbl;
CGRect frame;
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:AnswerIdentifier] autorelease];
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.height = [self textHeight:indexPath];
frame.size.width = 260;
lbl = [[UILabel alloc] initWithFrame:frame];
lbl.lineBreakMode = UILineBreakModeWordWrap;
lbl.numberOfLines = 0;
[cell.contentView addSubview:lbl];
[lbl release];
return cell;
}
- (CGFloat)textHeight:(NSIndexPath *)indexPath {
CGSize textSize;
CGSize constraintSize;
CGFloat height;
NSString *theText = [self cellText:indexPath];
constraintSize = CGSizeMake(260.0f, MAXFLOAT);
textSize = [theText sizeWithFont:kCELL_FONT
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
height = textSize.height;
return height;
}
The table obviously needs to fit in the frame of the containing view (which might be the top level view or the window), so I might use percentage or pixel offsets from that rect.
Try using
[[UIScreen mainScreen] applicationBounds];
This will return a CGRect for the current window size. Then, for your code, you might use:
CGRect screenRect = [[UIScreen mainScreen] applicationBounds];
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.height = [self textHeight:indexPath];
frame.size.width = screenRect.size.width - 60.0;
Assuming that the pixel offsets in x are fixed, with flexible label width.
I use UITableView with cells created using UITableViewCellStyleSubtitle. Every cell's height is dynamically adjusted using the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method.
The problem you can see on the picture
(note: image updated)
http://img.skitch.com/20090715-g7srxgee2d7fhi5rab2wufrdgm.png
How to set up align of textLabel and detailTextLabel to the top of the cell? (I really don't wont to do it by subclassing UITableViewCell and overriding layoutSubviews)
Thanx
Well, this one cost me an afternoon, but I think I figured it out. As far as I can tell, this appears to be a bug in how UITableViewCell is laying out the textLabel and detailTextLabel. When you set the row height, it seems to allocate equal height to the two labels, which means that you get exactly the behavior you're seeing above, even though detailTextLabel needs more room. Here are the two things I did to fix the problem. I had to subclass UITableViewCell to fix it, but it's a minimal amount of code.
First, make sure you're calculating the height of each row properly. Put this method into your table view delegate. Replace the font methods with your own:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellDetailText = [[self itemForIndexPath: indexPath] detailDescription];
NSString *cellText = [[self itemForIndexPath: indexPath] description];
// The width subtracted from the tableView frame depends on:
// 40.0 for detail accessory
// Width of icon image
// Editing width
// I don't think you can count on the cell being properly laid out here, so you've
// got to hard code it based on the state of the table.
CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 40.0 - 50.0, CGFLOAT_MAX);
CGSize labelSize = [cellText sizeWithFont: [self cellTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [cellDetailText sizeWithFont: [self cellDetailTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat result = MAX(44.0, labelSize.height + detailSize.height + 12.0);
return result;
}
Then, subclass UITableViewCell and override layoutSubviews:
#import "UITableViewCellFixed.h"
#implementation UITableViewCellFixed
- (void) layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x,
4.0,
self.textLabel.frame.size.width,
self.textLabel.frame.size.height);
self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x,
8.0 + self.textLabel.frame.size.height,
self.detailTextLabel.frame.size.width,
self.detailTextLabel.frame.size.height);
}
#end
Here is another solution without subclassing cells, so it certainly works with different table styles. (I haven't checked other solutions.) The title and detail strings are declared in my UITableViewController header and already defined. They aren''t very long, certainly well within height 4000!
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;
int section = indexPath.section;
int row = indexPath.row;
NSString *title_string = [title_strings_array objectAtIndex:row];
NSString *detail_string = [detail_strings_array objectAtIndex:row];
CGSize title_size = {0, 0};
CGSize detail_size = {0, 0};
if (title_string && [title_string isEqualToString:#""] == NO ) {
title_size = [title_string sizeWithFont:[UIFont systemFontOfSize:22.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
if (detail_string && [title_string isEqualToString:#""] == NO ) {
detail_size = [detail_string sizeWithFont:[UIFont systemFontOfSize:18.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
}
CGFloat title_height = title_size.height;
CGFloat detail_height = detail_size.height;
CGFloat content_size = title_height + detail_height;
CGFloat height;
switch ( section ) {
case 0:
height = content_size;
break;
//Just in case
default:
height = 44.0;
break;
}
return height;
}
However you're sizing your cells, you should do it with the various sizing methods of NSString. That way, you can determine exactly how tall to make the cell and avoid the whitespace.
If it turns out that the textLabel and detailTextLabel are laid out using autoresizing masks, maybe you can do this when you return the cell:
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
cell.detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
If this works, it's a bit easier than subclassing the cell. I haven't tried it though.