How to decrease width of table view cells for iphone - iphone

I'm trying to create a grouped table view with two sections. For the first section I would like the width to be only half the screen. For the second section it would be the standard width. Also, next to the first section I would like to put a button.
How is this done?
Thanks!

To put a button in a section what I've done in the past is create a section that has no rows in it. Then, I respond to the - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method with a view that contains a button that is rigged to whatever delegate I need. This will give you the appearance of having a button sitting in a section with no data. This is how buttons appear in the Contacts view/edit screens in the stock iPhone apps.
Table view sections are fixed with to the size of the screen. If you want the individual cells to appear narrower in one section and wider in another, then you can control the size of the view itself with the data source delegate, though you might have to set the background of the table view to transparent so users can actually see the smaller view on top of the table view.

Related

How do I initialize a UITableView in the middle of the table

How can I initialize a UITableView so that it starts off showing something (of my choosing) in the middle of the table, rather than what is at the top? I want to have a certain cell in the table always start at the top of the screen, so you could scroll up or down from there.
You can scroll to a certain cell by sending - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated to the table view.
As you have described you want the cell of your choosing to appear "in the middle of the table", so you should use UITableViewScrollPositionMiddle for the scrollPosition argument.
If you do this as soon right after your table is initialized or as soon as your view loads with the animated argument set to NO then the table view will appear with your cell in the middle, just as you've described.
You also write: "I want to have a certain cell in the table always start at the top of the screen".
By instead choosing that cell and the scrollPosition UITableViewScrollPositionTop you can make that cell be at the top of your table view.

Customize empty cells in a table view

I have a table view in which I would like to change the backgrounds of the cells. This can be done by subclassing UITableViewCell and setting the class of the cells in Interface Builder but this will not change the background of the cells used to "fill up" the table view. E.g. if I only have eight cells, then nine cells may be added to fill up the table view with cells.
Is there any way to change the background of these empty cells?
EDIT
This is not an issue regarding static table views. This applies to all table views. How to customize the background of an empty cell?
What I really wanted was to replicate the look of the iPad Settings app. I realized that Apple just hides the separators and does not set a background on their "empty" cells. This is what I ended up doing.
Setting a footer on the table view and then setting the height of this footer to 0.1f will hide the footer and not show any cells below it.

UITableView with dynamically set size

I have a UITableView as a subview in a ScrollView with other widgets around like a button. I'd like to put the button always at the end of the ScrollView and I'd like to have the UITableView to show dynamically more section. How and where shall I determine the Table size, correctly set it and visualize it?
From Interface Builder it seems that I can only set static size to the TableView (which of course limits the number of sections visible) and stick the button position to the bottom whether a rotation happens.
If you have only few simple controls after the table then I'd suggest putting them to the table itself and get rid of the unhealthy (in my opinion) combination of table view inside scroll view.
You might add the button you are talking about to the table's footer.
It may be done in the Interface Builder (drag-n-drop the button to the bottom of the table view) or in the code ([tableView setTableFooterView:myButton];).
If your button should be smaller that table's width then put it inside UIView and locate as you need.
You can also add table header in a similar way...

UITableView - centering of Sections

I am using a table view with its style set as "Grouped". I have only 2 sections to be displayed in the table view and the table view does not occupy full iPhone screen (It has some other views with it). So I would like to display those 2 sections centered in the table view rather than displaying the sections from the top of table view.
One round about way is to set header and footer of the table view appropriately. But the disadvantage of this is, each sections will be equidistant (looks awkward!). Anybody with solutions to it?
Thanks,
Raj

UITableView has a custom section header view which disappears

The tableview, custom table header, and custom section headers are loaded from a NIB. The tableview is grouped.
When the view loads, the first section header doesn't show up. If I scroll down, the other section headers will appear at first, but will disappear as soon as the section above them touches the top of the screen.
If I scroll back up so that a disappeared section header is off the screen, then scroll back down, it will usually reappear.
The problem is fairly consistent but not entirely- sometimes I have to scroll up and down several times to get a header to reappear. Any ideas as to what could cause this?
I fixed it- I was using one UIView for 3 sections, changing the text appropriately then returning it. Creating separate UIViews in the NIB for each section fixed the problem. So I guess you can't do that.
Your custom views must be (or descend from) UILable or UIImageView objects. You may need to manually set row height for the headers to get custom views to load and display properly.
You should review the details in the UITableViewDelegate protocol.
Specifically, look at these methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Part of the discussion on the tableView:viewForHeaderInSection: method states:
This method only works correctly when tableView:heightForHeaderInSection: is also implemented.