HurkNburkS
HurkNburkS

Reputation: 5510

How to insert a UITableViewCell at the beginning of UITableView

I am trying to set up a UITableView, with x amount of sections and X number of rows per section.

However I would like to add a single row to the top of my UITableView Is there is a way to hardcode this into the view?

I currently return the number of sections and rows per section based off a NSdictionary like so.

- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
{
    // Return the number of sections.
    return [letterDictionary count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // returns the number of rows per section based off each entry in letterDictionary   
    currentLetter = [sectionLetterArray objectAtIndex:section];
    return [[letterDictionary objectForKey:currentLetter] count];       
}

Upvotes: 0

Views: 275

Answers (3)

Qiming
Qiming

Reputation: 1744

You can add a "header" to the tableview.

In your tableview class:

self.tableView.tableHeaderView = yourView;

Upvotes: 1

matteodv
matteodv

Reputation: 3982

You could try to customize your tableview section's header...
For example you can use something like this:

YourController.h
-(UIView *)headerView;

YourController.m
-(UIView *)headerView
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectZero];
    // Place everything you want in your header
    // using [header addSubview:yourSubview];
    // Finally set header's frame and return it
    header.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
    return header;
}

// Use this to return header's height
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == yourSection)
    {
        return [self headerView].frame.size.height;
    }
    else
    {
        return [self sectionHeaderHeight];
    }
}

// Use this to return your view
-(UIView *)tableView:(UITableVIew *)tableVIew viewForHeaderInSection:(NSInteger)section
{
    if (section == yourSection)  // To show the header only on a specified section
    {
        return [self headerView];
    }
    else
    {
        return nil;
    }
}

If you change your mind and want to customize below the tableView, your can use the same method changing Header with Footer.
Finally take a look at docs about these methods:
- tableView:viewForHeaderInSection:
- tableView:heightForHeaderInSection:
- tableView:viewForFooterInSection:
- tableView:heightForFooterInSection:

Hope this fits your needs!

Upvotes: 0

user387184
user387184

Reputation: 11053

You cannot just add a row above your UItable within the table. If you just need a row of text why not use UITextField, UILabel or UITextView depending on your needs and just position it above your UItable whereever you like.

If I missunderstood you and you just like to add one row as the very first one in your first section, all you need to do sth like this:

if (section == 0) {
  return [[letterDictionary objectForKey:currentLetter] count]+1;
} else
{
  return [[letterDictionary objectForKey:currentLetter] count];
}

and ensure that when returning row for indexpath you have also a similar if-statement and return whatever you need for section == 0 and row == 0.

But this first row will certainly scroll away if you scroll down your table view - as said I am not sure exactly what you need.

Upvotes: 0

Related Questions