UIImageView in UITableCell gets a weird size - iphone

I'm trying to place a UIImageView in a UITableViewCell.
I create a special view called ProductView. In that view, I have several components, one of them being a UIImageView. I set this ProductView as content view of the UITableViewCell. Everything is drawn properly except the UIImageView. UIImageView gets scaled meaninglessly. It's height gets twice the height of the table cell size.
I've spent hours trying to solve this problem. I've set the frame of UIImageView manually, I've called drawRect method of UIImage in UIImageView, neither of them worked.
I'm thinking of replacing UITableView with a UIScrollView if I can't find a solution. Does anyone know how to solve this problem?

Use the Custom cells, and place all the UI component inside that cell, (including the UIImageView), and in cellForRowAtIndexPath method, set the image like following:
[cell.imgView setContentMode:UIViewContentModeScaleAspectFit];
[cell.imgView setFrame:CGRectMake(cell.imgView.frame.origin.x, cell.imgView.frame.origin.y, 120, 79)];
[cell.imgView setImage:[UIImage imageNamed:#"image.png"]];
And if you need to know how to use custom cells, follow this tutorial :
iPhone Programming Tutorial: Part 6: Creating custom UITableViewCell Using Interface Builder [UITableView]
Hope this helps you...

I've had the same problem and realized in the end that the issue wasn't anything to do with automatic resizing, it was simply that I was not getting the UIImageView, but its parent view (the table cell), due to clashing tag values (I was getting the view through callUIImageView *pictureView = (UIImageView *)[cell viewWithTag:0]; and tag zero was also set on the cell). Fixed by just changing the tag value.

Related

Custom UITableViewCell with Storyboard

My goal is to create a custom UITableViewCell which contains 2 UILabels. One is the title and one is the detail. The detail label should allow for 3 rows of text.
So I went on and created a custom table view cell in storyboard. I also created a subclass of UITableViewCell and linked the two together.
I the added two UILabel to the cell in storyboard and placed them where i wanted them to be and linked them to their coresponding outlets in teh subclass. Since the content of the labels varies I wanted to align the text vertically to the top. As I understand the only way to do this is by calling the sizeToFit method on the label. I execute this under in the sub class of UITableViewCell:
-(void)layoutSubviews {
[super layoutSubviews];
[self.detailTextLabel sizeToFit];
}
So far everything seems fine and the text in the detailTextLabel is aligned as it should. Although when i satrt interacting with the cell, for example slide my finger over it so the delete button appears, the detailTextLabel will change size to the size that was set in storyboard. This causes the text to be misaligned. Similar things happen when i select the cell and change to another view and the return to the table view via a tab bar
My question is: Is there any way of creating this custom cell differently using storyboard or is my only alterative to create everything programtically?
Regard, Christian
Maybe you should take a look at this if you still want to vertically align your text in your UILabel without sizeToFit (who will resize it when you will interact with your cell).
About your question, I think you can create your custom cell from a xib file like this.

Strange UITableViewCellStyleSubtitle + [cell imageView] behaviors

This question's been bugging me for a month and i've been resisting posting about it, but I can hold out no longer. Here's what's going on:
I have an app with UITableViewCells, all initialized as UITableViewCellStyleDefault. For each one, I am setting the image view's image using the excellent SDWebImage package, which extends UIImageView to have a method for [UIImageView setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)image].
The problem is that the images are showing up more narrow than they should be, on the tableView, but for some reason selecting a row makes it the correct size. So does scrolling the row off screen, then onscreen, then offscreen.
What method should I be using to force cells to redraw? I have found a few conflicting ideas and none of them have worked so far. I feel like there's a simple call to UITableView to UITableViewCell I'm missing out on. Also, where do I put this call? I have tried calls in cellForRowAtIndexPath and willDisplayCell .
Thanks for your help!
SDWebImage: https://github.com/rs/SDWebImage
I solved the issue - I had to just make my SDWebImage placeholder images the same aspect ratio as the images I'm trying to load. As my server forces all my images to be 95x95, this was a relatively easy fix; I simply updated my placeholder.png to also be 95x95 (instead of some odd dimension it was, like 92x132)
[[cell imageView] setContentMode:UIViewContentModeScaleAspectFit];
this line makes the imageview show the image in its original resolution but fit to the image view size.
the main drawback is if u have the images in different sizes then each image will show i different sizes.... it should not looks good...

Adding a dynamic-height UITableView into a scrolling view?

Hello all – I'm getting into iPhone development and have hit my first confusing UI point. Here's the situation:
My app is tab-based, and the view that I'm confused about has a static featured content image at the top, then a dynamic list below into which X headlines are loaded. My goal is to have the height of the headline table grow as elements are added to it, and then to have the whole view scroll (both featured image on top and headline list below). So, I guess my question comes in two parts:
1) First, how do you set up a dynamic-height table view that will grow as cells are added to it. So far I've only been able to have my tables handle their own scrolling.
2) Then, what is the root NIB view that the featured image and the table should live in to enabled scrolling? I've dropped oversized content into a UIScrollView now, although did seem to have any success with having it automatically scroll.
Thanks in advance for any help on this subject!
To the first:
As i understand your situation:
You want to add a image to the top of the UITableView and the image should scroll with the UITableView, shouldn't?
The UITabeView has a property called tableHeaderView. It's just a view, so you can set a UIImageView to it.
(I have no xCode at the current time, you need to edit the code)
UIImage *image = [UIImage imageNamed:#"myCoolPic.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame =CGRectMake(0,0,width,height);
tableView.tableHeaderView = imageView;
[imageView release];
What you're asking is probably doable with Interface Builder (or not, I don't know) but I know the code way to do it.
To change the height of the table all you do is set the frame of the UITableView object. The default height of a UITableViewCell is 44 I believe, so set it to multiples of that depending on how many cells you have. Of course your cells can be any height so you will need to keep track of what you report in heightForRowAtIndexPath and set the table frame accordingly.
UITableView will certainly live in a UIScrollView and both components can scroll. The table view needs to become a subview of the scroll view, so does the image. Then you will scroll the table if you drag on it directly or scroll the scroll view if you drag the image or the scroll view.
For the first question, I'm a little confused by the way you ask it: "how do you set up a dynamic-height table view that will grow as cells are added to it." Table views have a function that it calls before the table is fully loaded with data called "numberOfRowsInSection." So the number of cells is based on that function, and should you update the variable used to determine the return value of that function (usually [myArray count]) it should automatically find the right size for the whole table.
However, variable height cells are something that I found kinda tricky and I've solved it using the following:
There are some UIKit NSString additions that you might find useful.
http://developer.apple.com/iphone/library/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
Particularly the sizeWithFont: functions.
Table views also have a 'heightForRowAtIndexPath:' function that is called 'numberOfRowsInSection' amount of times. Each call determines the height of the cell at the indexpath.
So, for example: (assuming myArray is an array of NSStrings)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [[myArray objectAtIndex:indexPath.row] sizeWithFont:myFont];}
This will return a height based off of your actual data, piece by piece. There are other functions to specify how the text wraps and truncates, etc. as well.
It doesn't feel like a great solution because you end up fetching your data twice, once to determine the height, and then again when you configure the cell in 'cellForRowAtIndexPath:' However, it does work!
I've learned a lot in the past few weeks and have gone through a few iterations of addressing this problem. My first solution was to manually measure the table height, then set the table rect to display at that height, and finally to set the scrollView's content rect to encompass the the table and top feature. What that solution did basically work, I started encountering some display issues when branching out into new views with different toolbar configurations. It seemed that my manual frame size was interfering with iPhone's native content scaling.
So, I scrapped the manual sizing and went to just making that top feature block be a custom table cell that displayed within its own section at the top of the table. I made a hard logic definition that section 0 only had one table cell, and that cell was my custom layout that I linked in through Interface Builder. I was then able to get rid of ALL my messy custom scaling logic, and the whole system is cleaner, smoother, and works reliably.

How can I add a static background behind a UITableView programmatically?

I have a TableViewController and would like to have a static picture as a background, which doesn't scroll along.
The way that everyone recommends using
[UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundPattern.png"]]
doesn't work as it will
1.) move along and
2.) put the background pattern in every cell
I know how to do it in a XIB file (namely adding another layer underneath the TableView) but how do I do it programmatically from the TableViewController?
[myTblViewController.view insertSubview:myImageView belowSubview:myTblViewController.tableView];
That should work.
If it turns out that tableView is not a direct subview of the table view controller's main view, you can try:
[[myTblViewController.tableView superview] insertSubview:myImageView belowSubview:myTblViewController.tableView]; //Edited superview should be all lower case

UITableView problem with "Inset" property

My navigation-based-app's background looks like a paper on a cork bulletin board. My RootViewController is, of course, an UITableView. I want to have the TableView end on the paper and not on the cork. Some pictures here to explain it better:
alt text http://img528.imageshack.us/img528/3469/bildschirmfoto20091028u.png
This is how it looks when I scroll to the last row of the TableView. For this, I'm using the "Inset" property for "Content-Bottom" and "Scrollers-Bottom" in Interface Builder.
alt text http://img24.imageshack.us/img24/3469/bildschirmfoto20091028u.png
This is how it looks when I scroll to the top of the TableView.
alt text http://img28.imageshack.us/img28/64/35859544.png
And this is how I want the TableView to look when I scroll to the top.
How can I do this?
Thanks a lot ;-)
Yassin
You can make frame of your tableView with the smaller size.height parameter. It will do the trick.
Oh... Ok. I have no mac nearby right now so I don't want to post much code. I will try to explain. First of all, you should inherit your rootViewController from UIViewController, not UITableViewController. If you are using IB, you have to refer your main view to your RootViewController's view property. Then you can set background of you main view
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
or add a UIImageView to your view and set your background image there. Then you have to add a UITableView to your view and set it's frame at any size you want. Next step - to refer your new tableView to the outlet inside your class. Smth like that:
IBOutlet UITableView* myTableView;
in your viewDidLoad: method set your new tableView's background to clearColor
[myTableView setBackgroundColor:[UIColor clearColor]];
and the last one. all your self.tableView you have to replace with myTableView.
P.S. Oh, don't forget that your new tableView should has frame with x,y,width properties, identical to your self.view's ones and tableView's height property should be a bit smaller.
Or just use insertSubview:corkBoard atIndex:1 during viewDidLoad or whatever. The corkBoard name should be whichever view the cork board is on, and index counts how many views are below it. So 1 would be only one view underneath, the UITableView. Make sense? Of course if you have more views you want underneath it, just change the index count.
In my opinion, this works better than setting a frame. The frame would have an awkward whitespace below it, while this just puts the table behind the cork. Quite ingenious :)