how to add UIImage view inside table cells for iphone - iphone

i want to add a UIimabeView inside table view cells to disyplay images (5 images on tap)
no is it pissible to show Images in table cells??/

Yes it is possible. You can add it to the cell's contentView in TableView's cellForRowAtIndexPath method.
Alternatively, you can create a custom tableview cell if you want to avoid the hassle of adjusting the imageView's positions in the code and you can just set the images for these image view in the cellForRowAtIndexPath method.

If you want to add views to a cell, add them to the contentView of the cell:
UIImageView *myImageView = ...;
[cell.contentView addSubview:myImageView];

Related

Custom UITableViewCell resize label when setEditing is called

Okay here is a picture:
I've got custom UITableViewCell with UILabel.
It's obvious I should resize my custom UILabel in -setEditing method, but how to exactly calculate how much to resize it? And is there efficient way to resize reused cells?
Thanks!
You don't have to do anything in setEditing method if you have the right autoresizing mask:
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
So the first question is how are you adding the label to the UITableViewCell? If you are adding it to the cell's view, then that is where your problem begins.
UITableViewCells have a contentView property. This contentView is responsible for resizing its contents when the cell enters editing mode. It knows how to do this. The cell's view property does not. So, first you need to add your UILabel as a subview of the cell's contentView in initWithStyle:. The cell doesn't actually know how big it is in initWithStyle:, though, so you can set the frame of the label in layoutSubviews.
As for resizing the cell, you can do this in tableView:heightForRowAtIndexPath:
In this method, you can check the indexPath of the row and configure the height appropriately.

UITableViewCell with background image and dynamic height

I have a tableView that present a list of Books, each of the table cells includes "Book name" and "Book description". The cell's height is determined by the length of the book description, so cells have different heights.
Every cell also has a background image that of course starches according to the cell's height.
I'm drawing the background image in the cell drawRect as follow:
- (void)drawRect:(CGRect)rect
{
UIImage *bgImage = [UIImage imageNamed:#"cell_BG.png"];
bgImage = [bgImage stretchableImageWithLeftCapWidth:60.0 topCapHeight:60.0];
[bgImage drawInRect:rect];
}
This code works, the problem is the scrolling performance, it's not smooth as I would like it to be.
I noticed that the main problem is the changing height, this seems to trigger a drawRect call for all cells, including the reusable cells.
(When I tried to set the same height for all cells the scrolling performance improved drastically, but I must use dynamic height...)
Is there a better approach to do this so the table scrolling will improve?
You probably don't want to override drawRect for a UITableViewCell's view since drawRect is pretty darn expensive.
Try placing a UIImageView on top of your prototype cell and setting its image (you can do so programatically or just drag in interface builder). You can then set the image view's frame size to match the dynamic height of its respective UITableViewCell. UIImageView is optimized for images so this approach should run smoother.
You can set the image and frame size of your imageView in the cellforRowAtIndexPath method in you UITableViewController.
UPDATE:
You'll probably also want to set the content mode to scale and fit your image.
From UIView class reference:
"contentMode - Provides layout behavior for the view’s content, as opposed to the frame of the view. This property also affects how the content is scaled to fit the view and whether it is cached or redrawn."
For example redraw calls drawRect anytime the view's frame changes.
It can be set programatically:
UIView* view = [[UIView alloc] initWithFrame:CGRectMake...etc];
view.contentMode = UIViewContentModeRedraw; // for example
or just by selecting the view in interface builder and setting the content mode attribute in the attributes inspector. I think your looking for UIContentModeScaleToFit or something like that. Also if using interface builder check the struts and springs of the UIImageView in the size inspector.

UITableView inside UIScrollView – Table Cell is not selectable

Like said in the title, I have a problem with an UITableView which is in my app a subview of an UIScrollView. In my UIScrollview I have several images, labels, etc. and at the bottom my UITableView is set. I can fill the table view with values etc., but when I try to select a cell by touch, nothing happens.
To see the UITableView, you must scroll down a little bit in the UIScrollView. Maybe this is the issue? How do I solve this problem?
Thanks!
When I have to do this kind of thing I use to put all the content that is on the top of the UITableView in a UIView and set this view to the tableHeaderView of the tableView. For example:
UIView * vwHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
// set the content of the header
// ...
_tblView.tableHeaderView = vwHeader;
This way, this header will be part of the table scrolling and you won't need a scrollView. You can do the same with the tableFooterView.
UITableview is
Inherits from
UIScrollView : UIView : UIResponder : NSObject
So why do you put your tableview inside the hscrollview? Don't do this.
Then unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

What's the best way to update an image in my tableview image subview?

Specifically, how do you get the image subview from a uitableview cell?
My table view gets data from the web asynchronously. When I created the image view the first time, I added the view by:
[imageView setImage:image];
[cell addSubview:imageView];
in tableView:cellForRowAtIndexPath:.
After new data is retrieved, I call reloadData to refresh the table view. But to update the image, I'd like to get a handle of this imageView (if it exist) and update the image.
My question is: Given the cell, is there a way to get this image view (handle) directly , or do I have to loop through the subviews to find the specific image view?
Any answer is highly appreciated!
Lu
Set a tag on the imageView when you create it:
[image viewSetTag: uniqueIntValue];
Then when you want to grab the view use:
UIView* recoveredImageView = [cell viewWithTag uniqueIntValue];

UITableViewCell labels opaque=YES looks bad when clicking cell

I want my table cells to load fast, so I am setting all my UILabels inside my cell to be opaque=YES; This is fine, because I also set the backgrounds to white and it looks normal.
The problem comes when you click the cell, since the backgrounds of those labels are white, the blue selected color looks pretty bad when trying to highlight the cell. Is there a work around for this? Would setting the background color of those cells to clearColor just defeat the purpose of setting opaque?
There is something you have to consider. First, setting the labels to opaque is definitely the right way of getting good scrolling performance.
The proper way to do this is declaring a subclass of UITableViewCell and overwrite the setBackgroundColor method like this and forward the background color to each element of the cell:
- (void) setBackgroundColor:(UIColor *)color {
[super setBackgroundColor:color];
[titleLabel setBackgroundColor:color];
[imageView setBackgroundColor:color];
[timeLabel setBackgroundColor:color];
}
I used this as the file's Owner of the XIB where the tableview cell is defined and have connected the UI elements to outlets in this custom subclass.