Scroll UITableView Header? - iphone

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);
}
}

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.

add finishing separator on uitableview

I have a custom separator style (fairly simple):
[[UITableView appearance] setSeparatorColor:SOMECOLOR];
Now I want to have my tableview finish with a separator. Currently separators only appear between two cells, but I want to have a separator at the end.
see here:
any ideas how this could be done?
I usually make my own separator inside the table view cell. I do this with a UIView that spans the width of the cell and is 1 or 2 points high.
In your case, if you want the system separator, you would have to add a custom cell at the end which is all transparent and 1 point high. UITableView would then add the missing separator.
I understand it you want a separator at the end as well? You can add a footer view to achieve this effect.
Make a footer view with height of 0.0001. To do so simply implement the following tableview delegate method :
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.001;
}

Dynamic tableheader view

I want to use a tableHeaderView that is dynamic in which the content can be added when certain action occurs or removed as well. The header frame should respond to the change and resize it but I cant seem to find solution to this. When I add content or remove content from tableHeaderView, I cannot change the frame size of header view of the table and so the cells look odd.
Is there a way to do this ?
I got this working by re-assigning the tableHeaderView property of the table view after modifying the table header:
UIView *header = self.tableView.tableHeaderView;
// add/remove contents, set header.frame, ...
self.tableView.tableHeaderView = header;

iphone Custom table cell, UILabel needs to be scrollable

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.

How to hide this part of uitableview

I designed a uitable view like in the image below , I want to hide the part under the second section , which I write on it (<- I want to remove this part ->)
any suggestion to do that
If you coppied the code from someone, this element is the fotter view for the second section. Look for the method:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
end erase it. It should be out.
You can try creating the table with specific height in the Interface Builder or:
UITableView* pTable = [[UITableView alloc] initWithFrame:(CGRect)];
Maybe you can also specify that scrolling is forbidden in order to show the contents that you want:
pTable.scrollEnabled = NO;
You might want to implement tableView:heightForFooterInSection: and tableView:heightForRowAtIndexPath: and return heights for the footer of the current section and header of the next section to reduce the gap between the two sections. This of course, if there are sections are the current section. If there are no more sections, then you can just reduce the frame size.