UITableView Static Cells - How to limit rows displayed? - uitableview

On iOS 5
I Have a UITableViewController set to Static Cells. I only need 3 rows which will be filled with content for each row. So I put 3 UITableViewCell. All works fine but when I run the app, it always shows more than 3 rows. The rest of the rows is empty rows. How can I only display the 3 rows I intended to display.
Should I use Grouped style and custom the look?

set footerView to an empty view.
self.tableView.tableFooterView = [UIView new].
this works on dynamic cells. should work on static cells too.

implement these two methods in your UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
In fact, these codes are telling tableview that you don't need to render the seperator line for me anymore, so that it looks the empty cells won't be displayed(in fact , the empty cell can not be selected too)

Related

Spacing between cell of tableview?

I have to set spacing between each tableview cell say of 36 pts .I am using tableview rather than custom cell for creating the cell?
You can do this by having number of sections equal to number of items to display. Each section will contain only one row. You will then return transparent header for every section which will give illusion of cell spacing.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return <Your items count>;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return cellSpacingHeight;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * headerView = [UIView new];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
A table view isn't like a collection view with a complex and capable layout specification, you can't specify arbitrary layouts and spacings.
So, use a collection view, or, if you want to continue with a table view, use a custom cell and add an empty section into each cell. Set the height of your cells to an appropriate value.

Make UITableViewHeaderFooterView non sticky

I am having a common footer for all cells and I am setting it with
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
I know that by default footers are sticky to bottom of UITableView, I dont want footers to stick to bottom of table, what is best way to achieve this?
One way I know is to add extra row and show footer there, But I am looking for cleaner approach than this.
Can anyone help me with this?
You can set UITableViewStyle as UITableViewStyleGrouped.
Use this initializer.
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
You can set this on storyboard.
You can create an extra uitableviewcell and append at the last of tableview. In the numberOfRowsInSection: method you can add one 1 like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [objectArray count]+1 ;
}
in tableView:cellForRowAtIndexPath: method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexpath.row == [objectArray count]) {
//create footer cell
}
else {
//show normal cell
}
}
If you happen to have static height for footer and not changing the contentInsets, you can try this (assuming footer height is 44):
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, -44, 0)

Remove space at the top of first row in table view

In my application i'm using custom cell in table view to display images in all rows. But in my .xib file there is a big empty space comes at the top of the custom cell. Can anyone tell me that how remove that space in iOS7 (iPad)? (like how to change the Y axis of the custom cell)
This is an IOS7 related issue with UITableViewStyleGrouped and size of footer views in section.
If you set the footerView to be size 0 it defaults to a larger value, The solution I found was to set the footer to a very small non zero number
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.0000001f;
}
Try to set into your property inspector like this..
This might help you:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.hidden = YES;
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
Sometimes the empty space in the first row is the section header space. This functions eliminate it.

Adding Static Cells to UITableView Dynamically

I am creating a simple sign up/login screen. I am using static cells as follows:
The cells are static. I want that when I click the sign up button I add 2 more cells to the tableview. Is that possible considering that I am using static cells and not dynamic?
UPDATE:
I am trying to set the height to 0.0f inside the heightForRowAtIndexPath but it is blowing up.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // this line
if(cell.tag == 2)
return 0.0f;
return cell.frame.size.height;
}
When you reload the table and it asks for the size you would add 2 more to it. Just based on the row index you would decide to use the static cells or whatever else you are inserting.
Your question is not very clear but it appears that you have 2 cells in the image. That means you're trying to go from 2 to 4 cells once your signup code has run. Simply put the 4 static cells in your storyboard UITableView and implement the following method and call [self reloadData] to trigger it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (SIGNUP_CODE_HAS_RUN == YES)
{
// where SIGNUP_CODE_HAS_RUN is set to YES after you've run your signup code
return 4;
}
else
{
return 2;
}
}

footer text in tableview is not showing

I have a basic grouped table view where i have implemented the two methods for setting header and footer for each section. Now, the header text is showing and working just fine. But the text in the footer view is not showing. I have 3 sections and only want to add text to the first footer section. The code:
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if ( section == 0 ) {
return #"Lorem ipsum";
}
return nil;
}
Now the weird thing is that it seems that the table is actually making space for the text, it's just not showing. I think there might be some really simple explanation to this, but can't seem to find it:)
I guess you are having heightForFooterInSection method in your viewController. If you are returning any value here, it is the problem. If you are doing, change your code like
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == 0) {
return 30.0f;
}
return 0.0f;
}

Resources