2

I want to set only one UITableCell at the bottom of my UITableView. Something like this:

+----------------+
| +------------+ |
| |   cell 1   | |
| +------------+ |
| +------------+ |
| |   cell 2   | |
| +------------+ |
| +------------+ |
| |   cell 3   | |
| +------------+ |
|                |
|                |
|                |
|                |
|                |
|                |
| +------------+ |
| | Last cell  | |
| +------------+ |
+----------------+

But I do not know how to achieve this.

I have looked at:

but all of them are to set all the cells at the bottom of the UITableView and I am not able to do it for only one cell.

Is it possible to set the last cell at the bottom of the UITableView? How can I achieve it?

EDIT: I have tried with two sections as comments suggested but it duplicates the space for cell 1, cell 2 and cell 3 on the second section.

I want that the second section will only have Last cell and not the space equals to the height of the rest of cells. After that, I want to add a specific space to the footerView of the first section.

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 11,900
  • 18
  • 71
  • 151
  • Hi, Have you tried the footer view? – Alexandre Barbier Nov 17 '16 at 11:25
  • Is `LastCell` sopposed to scroll? – shallowThought Nov 17 '16 at 11:25
  • 2
    The last link has an interesting idea: Section 0 for cell1, cell2, cell3, Section 1 for lastCell. Then, add a empty headerView for Section 0 (of size 0,0), and and bigger one "transparent" that calculate the space to occupy between cell3 "end" and lastCell "start". – Larme Nov 17 '16 at 11:25
  • @shallowThought It should be at the bottom of the `UITableView`. It there is so much options that the last cell is not visible it should scroll to see it but if there is no need, it should be displayed inside the screen. – Francisco Romero Nov 17 '16 at 11:29
  • 1
    @abarbier if it added as a footer view it will stick on the last cell if the content size of the tableview is less than the screen size. – Ahmad F Nov 17 '16 at 11:29
  • @Larme So if I understand you correctly, should I create two custom cells in two different sections? But I need that the Last Cell should be with the same behaviour as cell1 and cell2. – Francisco Romero Nov 17 '16 at 11:32
  • The idea above is good, if you dont do it then you might want to just create a custom view to put exactly there if the (number of the cell * cell height) is lesser than (view height - 1 cell height) – Tj3n Nov 17 '16 at 11:32
  • @Error404 different section but still the same cell, why it have different behavior? – Tj3n Nov 17 '16 at 11:34
  • @Tj3n I mean. I want to create a menu. And the last option is to go back so I want it to be at the bottom of the space for the menu and separated of the rest of sections. – Francisco Romero Nov 17 '16 at 11:35
  • The key here is the spacer section header, not about the cell, you should have handle the cell behavior already, the section doesnt need more custom cell or anything like that – Tj3n Nov 17 '16 at 11:38
  • @Tj3n Yes, the functionality is working well. I only need the space between those cells. – Francisco Romero Nov 17 '16 at 11:40
  • you can add view as footer of tableview with same tablviewcell layout. – KKRocks Nov 17 '16 at 11:44
  • You can add empty cells with no separators. Or you could use sections and use footerView property of sections. Increase it as much as you want. – NSNoob Nov 17 '16 at 12:36

2 Answers2

1

I think what you need is something like this:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!
var array = ["1","2","3","4"]
var dummyInserted = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableView.reloadData()
    if array.count < 15 {
        array.insert("", at: array.count-1)
        dummyInserted = true
        tableView.reloadData()
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - UITableView DataSource
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

// MARK: - UITableView Delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier")

    cell?.textLabel?.text = array[indexPath.row]

    return cell!
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if dummyInserted == true {

        if indexPath.row == array.count-2 {

            return tableView.bounds.size.height - CGFloat(((array.count-1) * 44))
        }
    }

    return 44
}
}

So if you know that your table view can hold for example 15 visible rows (maybe you have to calculate that), then you can

1) if the last cell is visible, insert a dummy value into you array at the second last index

2) set the height of that dummy row to a height, that the last value is on the bottom of you table view

Maybe you have to change some code, but it should give you a good starting point of my idea

It should look like

enter image description here

Retterdesdialogs
  • 3,056
  • 1
  • 19
  • 37
0

Create two section for uitableview.Put The first items in FirstSection and another in the second section.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;
    switch (section)
    {
        case 0:
            sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
            break;
        case 1:
            sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
            break;
        // ...
        default:
            sectionName = @"";
            break;
    }    
    return sectionName;
}
NSNoob
  • 5,320
  • 5
  • 35
  • 51
Sandu
  • 409
  • 4
  • 8