How to avoid UIImageView contained in the UITableViewCell being expanded? - iphone

How to avoid UIImageView contained in the UITableViewCell being expanded when each table view cell has different height? I am having variable height for each row depending on the length of text coming for UILabel. When height of a UITableViewCell increases,the image views image height also increases. How to keep image views frame constant?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.myArray objectAtIndex:indexPath.row];
if(text.length!=0)
{
CGSize constraint = CGSizeMake(212, LONG_MAX);
CGSize size = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:14.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
return (71.0 + height); //this gives the row height
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MyXYZ *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyXYZ" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyXYZ class]]) {
cell = (MyXYZ *)currentObject;
] break;
}
}
}
((HJManagedImageV *)cell.myImageView).url = [NSURL URLWithString:myImageUrl];
}

Set contentMode property of your image view to appropriate value to disallow scaling of the image inside image view and do not care about image view itself being resized.

Create a custom cell in which you can keep the frame of the UIImageView constant. This will help it to remain the same throughout the tableView cells even if you vary the height of various cells

try this
[cell.contentView addSubview:imageview];

Related

Change Height of UITableView Customcell

I have implemented UITableView with Custom cell,
What i want to do is, i want to change height UITableViewCell according to text length,(Dynamic height)
here is my code snippet,
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [DescArr objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 55.0);
return height;
}
Height of UITableViewCell changes properly but the height of CustomCell does not change,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"BTSTicketsCellIdentifier";
CRIndCoupnCell *cell = (CRIndCoupnCell *)[tblDescriptn dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CRIndCoupnCell" owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CRIndCoupnCell class]])
cell = (CRIndCoupnCell *)oneObject;
}
NSLog(#"cell.frame.size.height=%f",cell.frame.size.height);
cell.lblleft.text=[arr objectAtIndex:indexPath.row];
cell.lblRight.text=[DescArr objectAtIndex:indexPath.row];
return cell;
}
My Log shows cell.frame.size.height=100.0 in all the rows. height of CustomCell doesn't change.
:
Where am i making mistake ? please help.
Thanks in advance
You are constraining the string to size of the full width of cell minus 2 times the cell margin and a max height of 2000 but since you are setting the string as text of the label with green colored text color, you should rather check for the size according to that label's width and an arbitrary height of 2000 or anything of your choice.
CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000);

Limit the size of an UIImage which is set programmatically

I'm dynamically loading images from a web service. The images will be loaded (with [UIImage imageWithData:data];) and be placed in a in an UIImageView in my custom UITableViewCell using the setImage message
. I gave the imageView a width and height of 27 by 19 in the interface builder and told him to "Aspect fit" (tried all the others too btw), but the image doesn't do that. It just scales (in aspect, I think) to fill the view cell.
I tried a lot of things, like resizing the image (does the job, but I can count the pixels on my retina display), resizing the UIImageView... But I just don't get is... Someone got an idea what I'm doing wrong?
The code that changes the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = self.tableCell;
self.tableCell = nil;
}
viArticle *article = [self.viData.articles objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = article.title;
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:#"%# %#", article.time, article.type];
if (article.image != nil) {
UIImageView *imageView;
imageView = (UIImageView *)[cell viewWithTag:0];
[imageView setImage:article.image];
}
return cell;
}
This is the view:
And the settings:
What happens if you untick "Autoresize Subviews"?
#GuidoH hey sorry for the late reply..
Here is the code that will add the image to the table view cell.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #" SimpleTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.imageView.image = image;
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
//use this delegate it will increase the row size and thus your imagesize will get increased too.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80; //value can be changed
}
I would suggest scaling the images will surely lead to slow performance by your app.
I just posted a "solution" earlier today, but that seem to work, but was a wrong assumption. After asking on IRC I found the real solution. I was using the tag 0, that one shouldn't be used. Changing it to an higher value really solved my problem. Sometimes it can be thát easy. :)
Thanks y'all.
If you want to resize the UIImageView you can use the frame property.
suppose you have a UIImageView declare in .h file(lets take UIImageView *myImage;) and it is linked to the interface builder too.
you have synthesis it in .m class etc.
then when you want to resize the UIImageView, use
myImage.frame = CGRect(x,y,width,height);
Note: by declaring the width and height in the code, the width and height declared in the IB will be override.
then dont forget to add it to subview,
i.e [self.view addSubview:myImage];
and its done, you can see the changes.

How to scroll UI tableview vertically and horizontally?

i have table view in my app and each row have long text so its possible to scroll whole table so user can read entire text
also i am using following code to set up my cell
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//buttonCount=0;
static NSString *CellIdentifier = #"Cell";
static NSUInteger const kLeftLabel = 100;
static NSUInteger const kRightLabel = 101;
row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect leftFrame = CGRectMake(2, 5, 45, 30);
CGRect rightFrame = CGRectMake(50, 5, 400, 30);
left = [[[UILabel alloc]initWithFrame:leftFrame]autorelease];
left.tag = kLeftLabel;
left.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:left];
right = [[[UILabel alloc]initWithFrame:rightFrame]autorelease];
right.tag=kRightLabel;
right.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:right];
}
else {
left = (UILabel*)[cell.contentView viewWithTag:kLeftLabel];
right=(UILabel*)[cell.contentView viewWithTag:kRightLabel];
}
left.text =[secondSplitArrayValue objectAtIndex:row];
right.text=[splitArrayValue objectAtIndex:row];
if (indexPath.row == 0)
{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone ];
}
return cell;
}
by use of above code i have two column so 1st column have small text but 2nd is very much big and to read i want to scroll it horizotally.
i hope some one will solve this problem.
thank you in advance
To solve your problem there is no need to modify scrolling of table view you can do it by following code... by using this code you can show ur text in multiline in the default label of table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = #"Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. ";
return cell;
}
You need to use the multiline label for the long text and set the height of your long cell properly by calculating with long text in below function.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
for more you can read the tutorial
place your UITableView in UIScrollview
But looking at your case there is no need to do such thing, I would prefer to make the cells height Dynamic according to the Text.
Just use above code which might help to solve your problem:
You need to use Custom Cell having TextView in cell;
//Calculate the Height & Width of Text
CGSize stringSize = [#"Your sample text whose height & Width need to be calulated" sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:UILineBreakModeWordWrap];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[Over here take TextView calculating the Height & width of String ];
// [add TextView it cells contentView];
[cell.contentView addSubview:TextView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
CGSize stringSize = [#"Your sample text whose height & Width need to be calulated" sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height;
}
if it doesn't solve your purpose then try using below alternative
Although I can suggest you to use the ScrollView & set it Delegate.
Add your UITAbleView as SubView to ScrollView & keep the size of scrollview according to the view how much you want to Scroll.
->View
-->ScrollView
--->UITableView

Fixing image-view in UITableViewCell in iPhone?

I have a table-cell which has all default settings. And I put an image on the ImageView on the left, and I put some random text on TextLabel.
and I adjust cell's height using below method.
(CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
The problem is the left ImageView's frame size is different depend on the cell's height.
See the below image, the 4th cell's height is bigger than others and it's image is slightly right compare to other images.
What causes this and how can I solve this probelm??
-- added
This is my cellForRowAtIndexPath method.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultt reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [self GetSelectionForRowAtIndexPath:indexPath];
NSString *path = [[NSBundle mainBundle] pathForResource:#"icon_notSelected" ofType:#"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
This shows the imageView does not get bigger even if I put more height on the cell.
UIImage *image = [UIImage imageNamed:#"your_image.png"];
cell.imageView.image = image;
put this code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
If will show the image in each cell of the table in same line.
You can do it only by Code. No need of IB for this.
Give the imageview a fixed frame and then center it vertically in the cell.
Something along these lines:
imageview.frame = CGRectMake(5, floor(cellHeight/2)-20, 40, 40);
Or just use the imageview property of UITableViewCell.

Custom UITableViewCell not appearing when row height is set

I have a custom UITableViewCell which I have created in IB. My labels display when I don't over-ride:
- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
however my content is squished. I want the height to be 120px so I have the following:
- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ CGFloat rowHeight = 120;
return rowHeight;
}
I'm not sure why the content inside of my UITableViewCell all of a sudden disappears?
UPDATE:
Here is how I create the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"ARCell";
ARObject *myARObject;
myARObject = [items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ARCell" owner:self options:nil];
cell = arCell;
self.arCell = nil;
}
UILabel *label;
label = (UILabel *)[cell viewWithTag:0];
label.text = [NSString stringWithFormat:#"%#", myARObject.username];
label = (UILabel *)[cell viewWithTag:1];
label.text = [NSString stringWithFormat:#"%#", myARObject.created_at];
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:#"%#", myARObject.articleTitle];
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:#"%#", myARObject.intro_text];
return cell;
}
within IB did you set the height of the view size there too?
if so you could just try to return 120 opposed to declaring it as a CGFloat
edit: one other thought, make sure you save in IB... thats the leading cause of display bugs!
I think there is something fishy going on with how you're loading the nib for the tableview cell. Instead of connecting the cellview to an outlet on the file's owner, why don't you try something like:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ARCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
At the very least I recommend you debug and set a breakpoint on that line to verify that
the cell really exists
the cell has the subviews you expect
the cell has the right frame and
bounds for the tableview cell