1

I would like to add a new cell when user taps or presses below the last cell in the UITableView, but only when the view height is greater than all cell's heights in all.

I do not want separators and I use:

myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

As a blank view instead.

I try to change and add a UITapGestureRecognizer to myTableView.tableFooterView. He also left at the end when there are enough cells to fill the screen. What I want is, that it works on the partly empty screen.

I do not think the UITableViewDelegate message is sent in this case.

EDIT

After @LeoNatan answer, I try with that (in viewDidLoad):

UIView *backView = [[UIView alloc] initWithFrame:self.myTableView.frame];
singleTapBack = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBack:)];
//singleTapBack.delegate = self;  // the problem...
singleTapBack.numberOfTapsRequired = 1;
singleTapBack.numberOfTouchesRequired = 1;
[backView addGestureRecognizer:singleTapBack];
self.myTableView.backgroundView = backView;

When I touch the background:

-[UIView indexPathForRowAtPoint:]: unrecognized selector sent to instance 0x7fff0bc16a20
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView indexPathForRowAtPoint:]: unrecognized selector sent to instance 0x7fff0bc16a20'

0x7fff0bc16a20 is UIView *backView
and @selector(handleTapBack:) is not called

Croises
  • 18,570
  • 4
  • 30
  • 47
  • 1
    Seems to me this would be a lot easier if you just designated the last cell in the section to be used for adding another cell, and that styling it would be trivial. – Jason Renaldo Dec 18 '14 at 21:24
  • @JayMorgan : I know. I propose a simple method to add cells. But I observed during the testing of new users, they touched below the list, in the empty part, with the idea to add something. And I want to give them the possibility. – Croises Dec 18 '14 at 22:30
  • @Croises: What was written in your `handleTapBack` method ? Issue is in that method. – Midhun MP Dec 19 '14 at 04:44

3 Answers3

2

You could add a new, empty, "Create new" cell at the bottom of the table. Do something like this.

@property (strong, nonatomic) UITableViewCell *createNewCell;

- (UITableViewCell *)createNewCell {
    if (!_createNewCell) {
        // can be your own nib too
        _createNewCell = [self.tableView dequeueReusableCellWithIdentifier:@"CreateNewCell"
        _createNewCell.textLabel.text = @"+ New entry";
    }
    return _createNewCell;
}

- (BOOL)isCreateNewCell:(NSIndexPath *)indexPath {
    return (indexPath.row == modelArray.count + 1);
}

- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self isCreateNewCell:indexPath]) {
        return self.createNewCell
    }

    // rest of cell configuration
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return modelArray.count + 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self isCreateNewCell:indexPath]) {
        // do insert stuff or go into edit mode
    }
    // do other selection stuff
}

This will make your table view have an extra cell at the bottom of the table all the time. You can write your own row inserting and editing.

If that is not what you want, you can make a background view that is transparent.

myTableView.backgroundView = [[UIView alloc] initWithFrame:self.tableView.frame;
myTableView.backgroundView.alpha = 0;

and add a UITapGestureRecognizer:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@sel(newCellTapped)];
[myTableView.backgroundView addGestureRecognizer:tgr];
Jadar
  • 1,643
  • 1
  • 13
  • 25
  • Thank you, but I do not want a bottom of the list cell. I already have a method to add cells. It's just that at the beginning the list is (too) empty. And I'd also like to add a cell when the screen is touched below the existing cells. But not after when the screen is filled with cells. – Croises Dec 18 '14 at 21:44
  • What does "when the View is greater than all cells." mean? – Jadar Dec 18 '14 at 22:19
  • Sorry for my english ... When the screen is large enough to display 10 cells. And that there are only 1 or 2 on the screen. – Croises Dec 18 '14 at 22:23
  • Ah ok. I've added to my answer at the bottom. – Jadar Dec 18 '14 at 23:06
  • I have already tried with footer height. But I do not want when the the screen is full. Because it then remains to the end. Or I have to delete it and add it again if the number of cells decreases. – Croises Dec 19 '14 at 00:06
  • 1
    Thank you very much Jadar ! It really works well. My error was: `singleTapBack.delegate = self;` I just erased this line. I do that in all my `UITapGestureRecognizer` without problem... – Croises Dec 19 '14 at 07:58
1

If you only want to be able to tap the empty areas of the table view, add a backgroundView to the table view. To that background view, add your gesture recognizer (remember to enable user interactions on the background view). When the gesture recognizer detects a tap, add a cell with an appropriate animation. Finally, check tableView.visibleCells.count to determine if your table view contains enough cells to fill the screen. If so, disable the gesture recognizer.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • I thought about doing that. My problem is that I have an automatic sizing of cells. And thus cells of different sizes. But it should not be necessary to disable the gesture recognizer once the background masked by cells. Or there is a risk of interference? – Croises Dec 18 '14 at 19:56
  • There are several ways to determine if the cells cover the table view. Depending on how you draw your cells, you may not even need to worry about it. – Léo Natan Dec 18 '14 at 19:58
  • Yes you might be able to just add a gesture recognizer to the background view. The table view usually intercepts all taps on cells, so the background view would never get it. It's worth a try! – Jadar Dec 18 '14 at 23:08
  • I try it (with various test and variations), but it does not work. Read my question edit (thanks!). – Croises Dec 19 '14 at 00:53
  • Check to see if the view object that is sent the incorrect message is the same as the background view you create. This looks strange. – Léo Natan Dec 19 '14 at 01:09
  • Yes, in my edit, `0x7fff0bc16a20` is `UIView *backView` – Croises Dec 19 '14 at 01:34
0

Well in this scenario below is what I would have done.

  1. Always load the tableview with your_size+1 (+1 is for adding empty cell)

  2. When tableview is loaded, create last cell which will have button of same size of cell.

  3. Call IBAction on this button press

  4. In IBAction, add cell and reload table

Do let me know if you have any questions.


If you are concerned about separator lines, visit below link.

Hide remove separator line if UITableViewCells are empty

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • Thank you. it was actually also my first idea. But it is difficult to maintain the last cell to exactly fills the screen (with cells that often change number and size). – Croises Dec 18 '14 at 21:49