Croises Croises - 5 months ago 21
Objective-C Question

Tap or LongPress below cells

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

Answer

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];