iphone Custom table cell, UILabel needs to be scrollable - iphone

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?

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.

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.

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.

Related

UITableViewCell content moving around

I have an iPhone app in which I'm using a UITableView to format a set of text responses, one per row. I've set up the cell in the storyboard to have a label inside it, and set up a constraint saying that the label should be 10 points from the edge of the cell. I then set up a custom subclass of UITableViewCell, set the cell in the storyboard to be of that class, and connected the outlet.
However, when I load the table, I see the text in the cell moving slightly to the right under some circumstances: when I select the cell, or when I load additional cells into the table. In fact, in the latter case, sometimes everything gets shifted to the right, even cells which were already there!
What the heck is going on here? The only changes I'm making in tableView:cellForRowAtIndexPath: are to the text in the label, and I'm always setting it. And I've unset "Indent While Editing" on the cell in the storyboard.
Answering some of the questions: I'm setting the view up using the storyboard. Xcode isn't reporting any ambiguity with the constraints. Also, here are the screenshots, before and after:
My guess is that the constraints for the label are ambiguous. Ambiguity can make UI components jump around for inexplicable reasons. You probably need to set more constraints for the label to define its position on both axes.
Or, maybe all you need to do is set the label to the "size that fits content" (intrinsic content size) under the Editor menu in IB.
Did you add a new label to the UITableViewCell, or are you working with the textLabel that already exists in it? If you added a new one, consider removing it and using the cell's existing textLabel property instead. If that's not an option for some reason, double-check that the label you've added is in the contentView of the cell, and that all the constraints are relative to the parent view, not to the cell itself.
Also, for debugging, you could set the cell's contentView background color to red (cell.contentView.backgroundColor = [UIColor redColor];) - this might give you a better sense of what's moving, the label or the whole view.
This is just a guess without seeing your code. I had a similar problem once, when the app reused an existing cell, the dimensions of the label were not correct. So, I had to remove the old label in my cellForRowAtIndexPath method, before adding a new label. Here's how I removed the old one:
UIView *oldLabel = [cell viewWithTag:3];
if (oldLabel != nil)
{
[oldLabel removeFromSuperview];
}
Then I added a new label like this:
[cell.contentView addSubview:newLabelOrWhatever];
It might be worth checking to see that your string content doesn't have a space prefixed on it. Also, you could verify the actual position of the label by setting the background color.
I had a similar issue when the label would move when the cell was selected. It was a custom cell that I was loading from a custom Nib.
In the Nib I had not set the backgroundView of the UITableViewCell (superclass) to any view. Once I set it (I set it to the ContentView) the issue stopped.
My auto-layout constrains seems fine and had no issues, so I'm assuming it was the above that fixed it.
If you have a custom subclass of UITableViewCell, try implementing layoutSubviews. Let me try from the top of my head:
static CGFloat const kLeftMargin = 10.0f;
- (void) layoutSubviews
{
[super layoutSubviews];
/* Prepare for processing */
CGSize cellSize = self.bounds.size;
/* Change the textLabel frame */
{
/* Compute the new label frame */
CGSize labelSize = self.textLabel.bounds.size;
CGFloat xCoor = kLeftMargin;
CGFloat yCoor = roundf((cellSize.height - labelSize.height) / 2.0f);
CGRect labelFrame = CGRectMake(xCoor, yCoor,
labelSize.width, labelSize.height);
/* Set the new label frame */
self.textLabel.frame = labelFrame;
}
}
Now, this isn't what is usually recommended (since a lot of people use Storyboards and NIBs), but from experience, if you want some correct layouting done, implement layoutSubviews.

UILabel does not wrap (if I change default font size)

I am working on a simple storyboard prototype. My TableViewController uses Dynamic Prototype as Content.
I have a cell with 4 label of which two will be set in code (the label text). The height of the cell will be calculated in code too. The Line Breaks are set to Word Wrap and everything's working fine with the default values (System 17.0):
see here:
..but if I change the Font Size of the "Fantasy Street..." label it will not break any more instead it just will be cut off!
see here: with System Font 16
Lines are set to 0
Word Wrap is still active
.. I also tried to do it manually in code but no change.
Does anyone have an explanation for that?
****edited:** when I add
myLabel.frame = CGRectMake(t.origin.x, t.origin.y, t.size.width, t.size.height *2);
to the cellForRowAtIndexPath I still see the cut off label. But if I then scroll the table view so the label is outside the viewable area shortly it will be displayed with the complete text when it is visible again.
By the way, I am working with viewTags, so I don't have a dedicated Cell Class e.g. UILabel *myLabel = (UILabel *)[cell viewWithTag:2];
You should check UILabel width; the width should be less than that of the value. Then like this:
(void) viewWillLayoutSubviews {
_landPhoneTips.preferredMaxLayoutWidth = _landPhoneTips.bounds.size.width;
}
I spent hours dealing with this identical problem before finally sorting it out last evening. After changing the font size, you must select the UILabel within the storyboard and select Edit > Size to Fit Content, even if you had already previously done so! In doing so you apparently reset some setting that gets messed up when changing the font size. Once done, the UILabel will wrap as it did previously.

Scroll UITableView Header?

I have a custom UITableView header which I want to be able to scroll above the top of the table. Normally the header would stick to the top of the table, is it not possible to somehow change to scrolling ability so that it can go beyond that and scrolls with the cells in the table?
Thanks.
Well if that is the desired behavior you want, just put the header as the first cell's content. It is possible to customize any particular cell you want. UITableViewDelegate documentaion will help you in that matter.
PS: the whole point of using tableView header is to make it stick to the top of the window.
EDIT: If it is necessary that you have to do the way you want, then you can try this: move your tableView a little down by setting its contentOffset. eg: myTableView.contentOffset= CGPointMake(0,heightOfYourView) . Now add yourView at the top
myTableView.tableViewHeader = myCustomTableHeaderView;
This would set myCustomTableHeaderView as the header of your table view and it would scroll with the table view.
By implementing tableView:viewForHeaderInSection: for section of index 0, you can instead have the header as the first section that should disappear when it scrolls. In this way, it's not a header for the whole UITableView.
If you only have one header for the whole table, can't you just set the tableHeaderView property?
You might, like me, have been searching for the tableHeaderView property. See this SO question for more info.
You should use Grouped TableView Style and in your ViewDidLoad method, add following line of code:
myTableView.backgroundColor=[UIColor clearColor];
myTableView.opaque=NO;
myTableView.backgroundView=nil;
Also in nib file, you should clear background color of grouped table;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}

UITableView change first upper/lower unused cell

Is it possible to modify the first "cell" under/over the "real" content?
Imagine this as the content or structure:
#0:[]
#1:[]
#2:[]
#3:[]
#4:[]
In this case, I would want to modify the cells with the number -1 and 5. But this should not affect the behaviour of the scrollbar. (e.g. not extending the scrollable content)
Thanks in advance
EDIT: Take a look at the facebook-refresh-cell. (Pull down the UITableView)
If I understand it correctly, you want to change the content of cells off-screen.
You actually don't want to change the cells themselves, but rather, you want to change the underlying data model that provides the cells with their data. You can, of course, do this at any time.
You shouldn't be trying to get at the UITableCell objects themselves.
Hi
Instead of trying to modify the cells, just create your own Footer and Header cells and put everything you need there, it will always stay there without modifying the actual data cells
Look into
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
}
you can do the same for Footer view just change the viewForHeaderInSection to viewForFooterInSection

assign text of a UIlabel to a text field

I am quite new to iphone development. I have a situation here. I have some labels which can be dragged across the screen.
What I want is when any of these labels are dragged to some textfield and released over a textfiels UIlabel test is assigned to that text field.
The crux in this is I have to check for a condition when UILabel is inside UITextfield.
Can you help me to fabricate this condition.
Thanx in advance
This might not be the best way, but it should work:
CGRect textfieldFrame = textfield.frame;
CGRect labelFrame = label.frame;
if (!CGRectIsNull(CGRectIntersection(textfieldFrame, labelFrame))) {
// the two objects' frames are overlapping
}
This only works I believe if the two objects are attached to the same view, but it should get you started.