开发者

iPhone - How add a final line in a UITableView to add items, preventing rearranging of this line

开发者 https://www.devze.com 2023-02-19 21:38 出处:网络
I have a UITableView put in edited mode. self.tableView.editing = YES; In this table view, I have some lines displayed in a custom cell, and I\'d like to add one at the end that would allow the us

I have a UITableView put in edited mode.

 self.tableView.editing = YES;

In this table view, I have some lines displayed in a custom cell, and I'd like to add one at the end that would allow the user to add an item (using another view).

So I wrote some lines :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return "n开发者_StackOverflow社区umber of lines" + 1;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) {   
        return UITableViewCellEditingStyleDelete;
    }
    else {
        return UITableViewCellEditingStyleInsert;
    } 
}

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) {
        cell.textLabel.text = @"Add a line...";
    }
    else {
        do the stuff in the custom cell
    }
}

Doing this way, the UITableView allows to rearrange any line. I can move the first line after the "add line", and move the "add line" in first position.

How may I remove the arrange button on the "add line" cell and prevent other lines to go under this one ?

Or is there a better way to code this ?


Implement

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([indexPath row] != INDEX_OF_SPECIAL_ROW)
}

and

- (NSIndexPath *)tableView:(UITableView *)tableView 
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS)
    {
        return proposedDestinationIndexPath;
    }

    NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0];

    return otherPath;
}


The much easier way would be to set a tableFooterView. You can place any UIView in there, so not only are you allowed to add a UITableViewCell but also a completely custom subclass. Doing it this way you will avoid all the unnecessary checks.


return UITableViewCellEditingStyleNone

For that row in - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

And check out – tableView:moveRowAtIndexPath:toIndexPath:

0

精彩评论

暂无评论...
验证码 换一张
取 消