How to display image and text in a tableviewcell - iphone

I have strings like this -
abcdefgh http://abc.abc.jpg adffggjk
The number of images is variable.
How can I display the image and text in a correct sequence in a tabview cell?

This can be achieved by customizing TableViewCell.
Design a data structure in that holds all your data(like a class with String array and image path array). Pass that to your custom cell and there add Lables and ImageView and subview for each string and Image in order you want.
If you have too much of data than you can adjust height of cell in heightForRowAtIndexPath method.
if you have too much of data then designing a UIScrollview and adding them in a Other ScrollView would be better idea.
Hope this is helpful...
Post if you need more help....

Related

UIImage size depending on UITableViewCell height

I have a UITableView whose cells have dynamic height. What I want to do is basically having an image that works as container for each tableview row like in the following picture. Since the content for each row is different, the image should be resized in height to contain the text of the cell.
I am wondering what is the best practice to achieve it.
Thanks in advance
Have a background image view for the cell and set the frame size based on the content you have to place inside the cell. Then you can use the "stretchableImageWithLeftCapWidth:topCapHeight:" method to create the image that you want to set the cell background.
UIImage *backgroundImg = [[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:20];
[cell.bgImageView setFrame:CGRectMake(a, b, c, d];
cell.bgImageView.image = backgroundImg;
Yo can use method sizeToFit for this image. else use the image type as center or scaleToFit.

how to subclass UITableViewCell if the content/height of each cell varies

I have a UITableView. The height and content of each cell of the table view varies, depending on the data feeding to the cell. To give an example - some might have pictures, some might only have text; some might have 1 picture, some might have more than 1 picture. So, I am thinking about subclassing the UITableViewCell so I can use the subclass in other table views that I might have.
Is there some sample code that I can follow to achieve that goal - subclassing UITableViewCell, varied content, varied height?
This has been asked many times.
Also, you don't need to subclass to get variable row height, nor to put custom controls into the cell, but for the latter case subclassing may be convenient.

iPhone - Custom TableViewCell height calculated off cell text

I'm creating a custom cell in the cellForRowAtIndex function and filling it with my dynamic text. The height of the cell is variable based upon this text so I need to user the heightForRowAt function, but in that function I don't have the cell. I can create a new one and fill it with the text and then call my calculateHeight function on it but then I'm creating 2 cells for each row.
Anyone have a better method? Cache the cells yourself in the cellForRow function in a dictionary by row index and use that? Is cellForRow called before heightForRow?
I've managed to crash Xcode4 twice so far playing around with this so am hoping someone has done this already.
tableView:heightForRowAtIndexPath:
is called before
tableView:cellForRowAtIndexPath:
Creating two cells is a possible solution, but creating two cells isn't a very effective method. However, you don't need the cell to calculate the height; All you need is the string.
You'll probably want to use
sizeWithFont:constrainedToSize:
on your string to calculate the height.
I could provide details on how to do that, but you should be able to figure it out from reading the documentation.
What you do is you make a class method on your custom cell (for instance), which knows the values of how things (statically sized things) will be set, and does some math to determine given the offsets of the text view for instance, it can tell you how big the cell needs to be after calculating how high your string will be given a font.

How to configure iOs UITextViews and UILabels so that their height is determined by their content

I am basically trying to find the correct way to design a UITableViewCell that has two UITextViews and three UILabels inside it. The two UITextViews have dynamic content and their size cannot be determined until runtime.
I have tried various methods of recalculating the height of my UITextViews based on the content that is loaded in them at runtime, and though very laborious and inane, I have gotten that to work, but they then bleed over the UILabels positioned in the xib below them.
Do I really have to recalculate the y coordinates of every UILabel after calculating the new size of the UITextViews? Is there a way to simply have the elements in the xib 'push' the elements below them down based on their new size?
What is the correct approach to configuring a UITableViewCell with multiple dynamic text views so that my entire application is not comprised of code to calculate heights and coordinates?
Or is the answer that I should not be putting more than one dynamic UITextView in the same UITableViewCell ?
I have used something like this for a cell to calculate its height in
tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
function
CGSize labelSize = [self.comments sizeWithFont:[UIFont fontWithName:#"Verdana" size:17.0]
constrainedToSize:CGSizeMake(280.0f, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
+20 is for padding.
and also if you are having every cell like the one you mention above, than you can create a class of UITableViewCell with following UIView elements inside it.
iOS doesn't seem to have any sort of flow layout engine -- at least none that I can think of.
If, however, your labels are located at the bottom of your UITableViewCell, you might be able to get away with setting the UILabel's autoresizingMask mask property to UIViewAutoresizingFlexibleBottomMargin so that the UILabels will anchor itself at the bottom. This is assuming that you already adjusted the size of the UITableViewCell to fit the entire controls. Otherwise, I think the best way to handle this is to recalculate the y coordinates of the labels.
I found it useful to add this sort of behavior as a category on UIView so a view can resize one of its subviews to fit some arbitrary text, and optionally resize the parent view to fit the newly enlarged subview as needed.
http://blog.carbonfive.com/2009/07/10/resizing-uilabel-to-fit-text/

Shifting UITableViewCell Content to the right

Is there a simple way to shift cell content to the right? Like a horizontal alignment or some sort of offset?
I'd rather not
include a blank cell.imageView.image, or
add spaces to each cell.textLabel.text.
Any ideas please. Thanks.
Create a UIView that contains subviews positioned where you want, i.e. set their frames. Then set the cell's contentView property to this parent view.