Moving table view cells down without moving tableview's origin - objective-c

I want to be able to move/shift the group of UITableViewCells in the UITableView downward, without scrolling the tableView, and without moving the entire tableview downward. The desired effect is that there is an amount of whitespace above the table view cells after they're placed in the table view, and when the user scrolls down through the table view, the white space is covered by the cells.

Don't forget that a UITableView is a subclass of UIScrollView.
And that UIScrollView have this property.
contentInset
The distance that the content view is inset from the enclosing scroll view.
#property(nonatomic) UIEdgeInsets contentInset
Discussion
Use this property to add to the scrolling area around the content. The unit of size is points. The default value is UIEdgeInsetsZero.
I don't fully understand what you are trying to do, so I just hope that this is what you are looking for.
After Question was Edited
Add an empty cell where you want it to be and adjust it's size when needed.
You will need to make some try about doing it animated (using insert and reloadCell) or using the reloadData option of the tableView to get the visual you want.

You could use a tableView header and adjust its size. After changing its size, call:
[tableView reloadData];
If you want to animate the size change, try calling:
[tableView beginUpdates];
[tableView endUpdates];
After changing the header's size.
EDIT 1:
As opposed to a table view header, you could use a static cell as mentioned in the other answer. To animate the size change, use the steps above. Change the size in:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Related

Make a UITableView that is not 100% width

I would like to make a grouped UITableView where the cells are not 100% width, this is because I want to show the background, and each cell will have rounded corners.
Sort of like this;
However, I'm not sure how to do this using Storyboard; or is it only something you can do in code?
Ideally, I'd like the whole area scrollable; but make the cells appear less than 100% width
One possibility: you could use a container view. That is, in the view controller where you want this table view to appear, add a Container View in interface builder and set the size however you like. Then add a UITableviewController to your storyboard. Embed the UITableviewController in the Container View by control dragging from the Container View to the UITableviewController and selecting embed.
i think you can use customized cells, set special backgrounds (with rounded corners etc.) and replacing views to have a padding to the borders. but not sure if it really works well. in the example you would have 3 different cells in one section.
Not sure if this is the best solution; the one I have so far is...
In storyboard
Create UITableView as normal as child of a UIView, set up
delegates, data source as required
Add a UITableViewCell to this
Create my own custom subclass of UITableViewCell;
- (void)setFrame:(CGRect)frame {
frame.origin.x += kInset;
frame.size.width -= 2 * kInset;
[super setFrame:frame];
}
-(void) drawRect:(CGRect)rect {
[self.layer setCornerRadius:kCornerRadius];
[self.layer setMasksToBounds:YES];
}
The constants are just any number for now, say 10
Next I ensure my UITableViewCell is pointing to this subclass.
Now my cells appear with a margin and the uitableview itself is 100% width.
I will have to keep playing around with it; maybe there is a better solution

scroll last Cell to top of UITableView

on my last cell is a dynamic textview. With bigger contentSize it will be stay on top of the tableview (to see it, while you fill it on keyboard), but if the textview and also the cell is going to be dynamically bigger: [tableview beginupdates] and [tableview endUpdates] will delete my contentSize. And after that it is scrolling it down to bottom of tableview.
Any ideas how to scroll the cell to the top, and create the cell also dynamically without changes on contentSize? maybe I'm thinking on the right way, ideas?
EDIT
Hmm..not any "correct" solution found...the best way for this purpose is to try set any new cells or sections below the textview to get this cell easy to top of the view. After this its easy to use it with beginUpdates and endUpdates. The cursor will be always in the textview, textview will be dynamically bigger also the cell, so the only easy way.
Another option is to set the uitableview as a subview of another scrollview (uitableview himself has one) to scroll up an down in the new scrollview.
Thanks for help.
From my experience, Text views in cells is a very tricky issue. I recommend just using the free Sensible TableView framework, which has text view cells available out of the box.

Change UITableViewCell frame but didn't work

UITableViewCell *cell = [tableViewController.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y - 34, cell.frame.size.width, cell.frame.size.height)];
I want to change the frame of table view cell, the code work in some ViewController, but some it doesn't work.
I want to know is there something wrong with it.
thanks a lot.
You cannot have full control on the cell frame, especially on the origin. The table view delegate is asked for a table view cell, but then its position depends on the internal logic of the table view in order to correctly place the cell inside the table view (which is a scroll view subclass). When the displayed portion of the table view is refreshed, the cell position is rearranged and the frame origin is updated.
So while your code is not wrong, it conflicts with some table internal rules you cannot fully control and so the results could unpredictable.
If you want instead to see a visible effect of this change, you can move your frame changer code to another part of the code, e.g. inside the tableView:didSelectRowAtIndexPath:
In such case you can for example touch a cell, call this code and change the cell frame and you will the change immediately. But as soon as you scroll the cell out of the iPhone screen, and then you move back this cell in the screen, you will notice that it will re-appear in its original position.

Animate section footer changes in grouped UITableView

I know that you can animate insertions of table view cells and section but sometimes I need to change the footer text to one with a different text or to one that is 'nil'. I need to animate it when a UISwitch is toggled.
Right now I am using [tableView reloadData] but this is ugly and changes the section footer too sudden and without any animations. Apple somehow animates this for example when you turn on Personal Hotspot the section footer is slightly changed and this change is animated.
How can I achieve the same effect?
Assuming you've already written the code in -tableView:titleForFooterInSection: to adjust in response to the UISwitch, you could just send an empty update block to the table view to get it reload with animation.
[self.tableView beginUpdates];
[self.tableView endUpdates];

UITableView w/ translucent section headers & different colored section backgrounds

I'm trying to set up a UITableView with elements layered like this:
Specifically, the table has multiple sections and each section has a different background color. To do this I would normally just modify each cell. The tough part is that I'd like the section headers to be translucent. When I do this, the background under the header is the table view's background color when there's no cell under the header. I could of course set the table view's background color, but then the color under each header would be the same.
How would I create a table view like that depicted in the diagram?
UPDATE: To make it absolutely clear, I know how to make a custom header view, and know how to make it translucent using the alpha property. The problem is with what is UNDER the view. I need what is underneath to be the section's background color, not the table view's background color.
There is no exact framework provided solution for this but we can make use backgroundView property. Set it to a scroll view. This scroll view will contain background views for section. Define yourself as observer for the table view's contentSize and contentOffset and alter the scroll view's values parallel to table view's. Get the frame rects for your background views using rectForSection: method and create subviews to the scroll view with appropriate background colors set. This should work to an extent.
The problem with this approach is that that the scroll view will remain static when the table view bounces on the edges. Then there is the case of getting the cell to blend well with this background.
Use a UIImageView in the header and use a (custom) image.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * junkView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headerImageBackground.png"]];
junkView.alpha = .3;
[junkView autorelease];
return junkView;
}