Detailed view controller in Objective-C - iphone

I'm having some trouble understanding how I should implement a detailed view controller for a table view, when I don't know how long/short the content is going to be.
Think of an RSS app. The main table view shows all the items, and when you click, you're supposed to get the contents of that item/article. How could I solve this using table views for the application, when table cells have a static height?
I'm using storyboard and segues for the application.

For your detail view you could place some or all of your content in a UIScrollView.
By the way, table cell heights are not static - just implelemt heightForRowAtIndexPath to calculate the height from your model data. If the cell contains text you will need to calculate the size given the required font. (Theres an answer on how to do this on StackOverflow here )

you can use following method to calculate dynamic height of row and lable as well.Use it in detail view.I think it will resolve your query.
Use it in heightForRowAtIndexPath for setting cell height and in cellForRowAtIndexPath for setting height of lable.
-
(CGFloat) GetHeightFoText:(NSString *)aStrTxt FoWidth:(int)aIntWidth ForFontSize:(int)aIntFntSize
{
CGSize maximumLabelSize = CGSizeMake(aIntWidth,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize:aIntFntSize]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}

Related

height of the text view to be dynamic inside a scrollview

I have a text view inside a scroll view, I want to let the height of text view be dynamic as its content, because I don't want to scroll the text view alone, In fact I want to scroll the text view with the scroll view together.
In fact, I confused with the right menu of the image above, about the constraints height equals, less than, bigger than,...
I don't want to let the height stable, I want it dynamic, in my code:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
200 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
which detailTextView is the name of text view, and scroll is the name of the scroll view.
But it doesn't work properly, what shall I do please help!
Note: Is there any relation between this issue and the checkbox of (use AutoLayout), because it is checked in my application
What the problem is? I have seen only one issue that you need to disable the scrolling of youtTextView.
CarinaM
Use following method for getting dynamic height of textfield as per text.
-(CGFloat) GetHeightFoText:(NSString *)aStrTxt FoWidth:(int)aIntWidth ForFontSize: (int)aIntFntSize
{
CGSize maximumLabelSize = CGSizeMake(aIntWidth,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize:aIntFntSize]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}

Resizing TableViewCell to suit RSS Feed Detail

I'm working on an RSS Feed and I'm looking at resizing the TableViewCell to suit the Title/Description information that comes in, I'd like the Cell to show all the text rather than cut it off... Does anybody know a good way of doing this or know of a good tutorial?
Thanks.
You need to calculate the height for the cell holding your feed item data and return it when the table asks for it in tableView:heightForRowAtIndexPath:.
Furthermore you need to say the UILabel not to truncate your text. For this, check out the methods under Sizing the Label’s Text
You should make custom UITableViewCell for that.Then set it correct autosizing parameters in your .xib file
And then, in your cell .m file you should implement layoutSubviews method in such way
- (void) layoutSubviews
{
[super layoutSubviews];
CGRect newFrame = rssLabel.frame;
CGSize maximumLabelSize = CGSizeMake(rssLabel.frame.size.width,9999);
CGSize expectedLabelSize = [rssLabel.text sizeWithFont:tweetLabel.font constrainedToSize:maximumLabelSize lineBreakMode:rssLabel.lineBreakMode];
int height = expectedLabelSize.height;
newFrame.size.height = height;
rssLabel.frame = newFrame;
}
And surely, I forgot to say that you should calculate the height of your row
The best approach is to subclass UITableViewCell. In this class implement a class method to return you height of cell. in tableview's delegate method tableView:heightForRowAtIndexPath: call this method and pass your text to it. Use NSString's sizeWithFont constrainedToSize lineBreakMode: method and calcilate height. set your label's property noOfLines to 0. And you're good to go.
For example:-
+(CGFloat)heightForText:(NSString *)value{
CGSize size = [value sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(320, MAXFLOAT)];
if(size.height<MINIMUM_CELL_HEIGHT){
return MINIMUM_CELL_HEIGHT;
}else{
return size.height;
}
}

Get width/frame of entire UITableViewCell

I have a method where I have a (rendered) UITableViewCell*. I would like to know the width/frame of the entire cell.
cell.frame doesn't seem to work - it gives me the frame of a random cell. The closest I got is cell.contentView.frame but it doesn't include the area covered by the accessoryView (see http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7). cell.contentView.superview.frame doesn't work either - it gives me a frame at some arbitrary location just like cell.frame does.
cell.contentView.width + cell.accessoryView.width works except in cases where the accessory view is UITableViewCellAccessoryNone
Any ideas how I can get the entire frame/width of the UITableViewCell in all cases?
Try this,
NSLog(#"Cell Width = %f",cell.frame.size.width);
NSLog(#"Cell Height = %f",cell.frame.size.height);
It will show the current cell Width and Height.
This code gives you the frame of the particular cell selected by indexPath:
// Get the cell rect and adjust it to consider scroll offset
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
cellRect = CGRectMake(cellRect.origin.x - tableView.contentOffset.x, cellRect.origin.y - tableView.contentOffset.y, cellRect.size.width, cellRect.size.height);
cell.frame will always give you the frame of the cell you're messaging.
And even if it did return a random cell, you should still be able to use the width of the frame, because all the cells have the same width.
I think by default, a cell will use 100% of the width of the UITableView so a UITableView's width is equivalent to a cell's width and because you would likely have set your table cell height using:
[UITableView setRowHeight:tableCellHeight];
Where tableCellHeight is a number of your choice and UITableView is the name of your UITableView not the UITableView class itself.
Then you would have both the width and height of a table cell, or did I misinterpret your question?

How to calculate dynamic size on scrollview for iphone?

I have dynamic no of textviews and their size can also be dynamic, after each text view there are also dynamic no of labels, and each item is place on scroll view, so that scroll view also has dynamic size, So Someone guide me how to accomplish this task?
forgive me if this is repetitive question plz!
For setting dynamic height of UILabel or UITextView, you can implement following method
This example is for UILabel, Remember, you need to set noOfLines property before setting dynamic height, you can set noOfLines to max number.
NSString *text = #"Your text here";
CGSize size = [text sizeWithFont:lblName.font constrainedToSize:CGSizeMake(lblName.frame.size.width, 10000)];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, size.height);
Hope this helps
What you need,
1 calculate textsize which you are going to to show on differnt controls.
for this use this line
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
2 Also use labels instead of textView at each place if you only need to show text.
because textViews justify the text means your same line can be fit in one line but also in two lines
3 set the scrollView contentSize as above answers says.by adding all textSizes with consider some spaces between various controls.
you can set the size of scrollview using setContentSize: and query size [scrollView contentSize]

iPhone -> dynamic cell height

I have a UITableView and a Cell which I design with Interface Builder.
I have two labels there.
The problem is, that one of the labels is text which can be 500 words or 10 words.
So I need a dynamic cell height. How can I handle this, because otherwise I have empty space or the cell height is not enough.
Perhaps someone has an idea how to do this?
Best Regards.
To know the size of your text, you can use this method:
- (CGFloat)measureTextHeight:(NSString*)text fontName:(NSString*)fontName fontSize:(CGFloat)fontSize constrainedToSize:(CGSize)constrainedToSize {
CGSize mTempSize = [text sizeWithFont:[UIFont fontWithName:fontName size:fontSize] constrainedToSize:constrainedToSize lineBreakMode:UILineBreakModeWordWrap];
return mTempSize.height; }
Then you can set your size cell with:
-(CGFloat)tableView:(UITableVIew*)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath;
-(CGFloat)tableView:(UITableVIew*) tableView heightForRowAtIndexPath(NSIndexPath *) indexPath
{
//here u can check the row number and ur labels and return ur custom height.
}