Add cell to bottom of UITableView in iOS

后端 未结 2 1700
生来不讨喜
生来不讨喜 2020-12-31 16:58

I am using xcode 4.2 with storyboard to create an iphone app.

When I press the edit button in the top right corner I would like to have the options to delete the exi

相关标签:
2条回答
  • 2020-12-31 17:40

    This tutorial is worth a read and should help you. It shows how to setup an 'Add new row' UITableView row at the bottom of a UITableView, but you should be able to make this appear at the top of your UITableView within your implementation of:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    e.g.

    if(self.editing && indexPath.row == 1)
    {
        cell.text = @"Insert new row";
        ...
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-31 17:53

    Ok, the basic idea is that when the edit button is clicked we'll show the delete controls next to each row and add a new row with the add control so that users can click it in order to add an entry right? First, since you have the edit button setup already let's instruct our table that in editing mode we should show an extra row. We do that in our tableView:numberOfRowsInSection:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.editing ? a_recs.count + 1 : a_recs.count;
    }
    

    a_recs here is the array I've setup to store our records so you'll have to switch that out with your own array. Next up we tell our tableView:cellForRowAtIndexPath: what to do with the extra row:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier = @"Cell";
        BOOL b_addCell = (indexPath.row == a_recs.count);
        if (b_addCell) // set identifier for add row
            CellIdentifier = @"AddCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            if (!b_addCell) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
    
        if (b_addCell)
            cell.textLabel.text = @"Add ...";
        else
            cell.textLabel.text = [a_recs objectAtIndex:indexPath.row];
    
        return cell;
    }
    

    We also want to instruct our table that for that add row we want the add icon:

    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == a_recs.count) 
            return UITableViewCellEditingStyleInsert;
        else
            return UITableViewCellEditingStyleDelete;
    }
    

    Butter. Now the super secret kung fu sauce that holds it all together with chopsticks:

    -(void)setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:animated];
        if(editing) {
            [self.tableView beginUpdates];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
            [self.tableView endUpdates];        
        } else {
            [self.tableView beginUpdates];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
            [self.tableView endUpdates];
            // place here anything else to do when the done button is clicked
    
        }
    }
    

    Good luck and bon appetit!

    0 讨论(0)
提交回复
热议问题