iphone Custom table cell, UILabel needs to be scrollable

435 views Asked by At

I have a custom table cell with a UILabel in. What I want can be done with two ways.

1- Find the height of the Label that contains long text and set the rowHeight to make the whole text visible.

2- Make the UILabel scrollable so the user can move the text up/down with his hand to read the whole string.

Can you please advice me on which one to do an how to do it?

3

There are 3 answers

0
Gilad Novik On BEST ANSWER

If your text is static and won't change dynamically, you should use the first option. You can cache the result so whenever you refresh the table, you won't have to recalculate the text. You can use sizeWithFont:constrainedToSize:lineBreakMode to calculate the actual text size.

For the row height: you should use tableView:heightForRowAtIndexPath: and not rowHeight, since rowHeight will change the height for ALL rows - not only a single cell.

A far as a scrollable text view inside a table: in my opinion - it looks like a cheap solution in an app and makes it a bit amateur.

2
Dima On

I recommend not using the first option in most cases as depending on the length of the string it could make your table cells huge.

As for the second option: You should be able to add a UIScrollView as a subview into the table view cells. The Scroll View can contain the fully sized text labels and let you scroll through them within a fixed size cell.

0
Martol1ni On

Do we have any example on how to do it? Im new to iphone, sorry for bugging. – Panos 9 mins ago

UIScrollView *scroller = [[UIScrollView alloc]initWithFrame:cell.frame];
scroller.contentSize = CGSizeMake(280, 100);
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
test.text = @"This would be your label.";
[scroller addSubview:test];
[cell addSubview:scroller];

This will make a UIScrollView and a label in the tablecell 'cell'. This will work, but I do not think this is the best solution, since it's gonna be a scrollview inside a tableview which already provides scrolling. My advice is to adjust the rowheigth.