简体   繁体   English

UITableView在所选行下方添加行

[英]UITableView add row below selected row

I had a working tableview where I could double-tap a cell and it would add an 'action' cell below which had four buttons programmed on it. 我有一个工作表视图,可以双击一个单元格,它将在下面添加一个“动作”单元格,并在其上设置了四个按钮。 Today I added alphabetic sections to the tableview and a section index and I can no longer get this functionality to work. 今天,我在表视图中添加了字母部分,并添加了部分索引,但我无法再使用此功能。 I've added a whole range of NSLogs to the code to try and find the problem and I can't, it seems to be trying to add a row in the same section and one row further down than the cell tapped, so I'm not sure what the problem is. 我已经在代码中添加了整个NSLogs尝试查找问题,但我做不到,它似乎试图在同一部分中添加一行,并且比所点击的单元格更下一行,所以我我不确定是什么问题。 Would anyone have any idea what I'm doing wrong? 有人知道我在做什么错吗? If anyone can shed any light on this I would be hugely appreciative. 如果有人能对此有所启示,我将非常感激。 (And apologies if my code is cumbersome or hard to follow, I'm new to this so feel free to suggest what I can improve!) (如果我的代码繁琐或难以理解,我深表歉意,我对此并不陌生,所以请随时提出我可以改进的地方!)

- (void)viewDidLoad
     {
     //I start with an array of objects, so I created two arrays; one containing the first letters of each Name, and a list of the number of objects that start with each of those names.
   nameIndex = [[NSMutableArray alloc] init];
   nameIndexCount = [[NSMutableDictionary alloc]init];   
    }

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
     return [nameIndex count];
      }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
     if (self.actionRowIndexPath) {

     //Returns first letter of the section
     NSString *alphabet = [nameIndex objectAtIndex:section];

     //Returns the number of entries starting with that letter
    NSString *numberofRows = [nameIndexCount objectForKey:alphabet];
    int intNumberOfRows = ([numberofRows integerValue] + 1);

    return intNumberOfRows;
} else {

    NSString *alphabet = [nameIndex objectAtIndex:section];

    NSString *numberofRows = [nameIndexCount objectForKey:alphabet];
    int intNumberOfRows = [numberofRows integerValue];

    return intNumberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newTableViewCell"];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newTableViewCell"];
    }
    //Configure the cell
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.frame];
    UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
    imageView.image = image;

    if ([indexPath isEqual:self.actionRowIndexPath]) {


        // Four UIButtons coded programmatically

    } else {


        Contact *p = [[[ContactStore sharedStore]allContacts]objectAtIndex:totalNumberOfRows];
        NSString *firstAndLastName = [NSString stringWithFormat:@"%@ %@", [p firstName], [p lastName]];
        indexPath = [self modelIndexPath:indexPath];
        cell.backgroundView = imageView;
        //        [cell.imageView setImage:smallThumbnailImage];
        [cell.imageView setImage:[p thumbnail]];
        [[cell textLabel]setBackgroundColor:[UIColor clearColor]];
        [[cell textLabel]setText:firstAndLastName];
        [[cell detailTextLabel]setBackgroundColor:[UIColor clearColor]];
        [[cell detailTextLabel]setText:[p phoneNumber]];

        totalNumberOfRows = totalNumberOfRows + 1;
    }
    return cell;
}

    #pragma mark - Action Row Support

-(NSIndexPath *)modelIndexPath: (NSIndexPath *)indexPath
{

    if (self.actionRowIndexPath == nil) {
        return indexPath;

    }

    if ([indexPath row] > [self.actionRowIndexPath row]) {
        return [NSIndexPath indexPathForRow:([indexPath row] - 1) inSection:indexPath.section];

    }
    return indexPath;

}

 - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {


    NSLog(@"Double tap");

    CGPoint p = [recognizer locationInView:self.tableView];

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];


    NSIndexPath *pathToDelete = self.actionRowIndexPath;

    _selectedIndexPath = [self modelIndexPath:_selectedIndexPath];

    //Is the user deselecting current row?
    if (_actionRowIndexPath) {
        [self.tableView deselectRowAtIndexPath:_selectedIndexPath animated:NO];
        self.selectedIndexPath = nil;
        self.actionRowIndexPath = nil;
    } else {
        //Selecting a new row
        self.selectedIndexPath = indexPath;

        self.actionRowIndexPath= [NSIndexPath indexPathForRow:([indexPath row] + 1) inSection:[indexPath section]];
    }

    [self.tableView beginUpdates];
    if (pathToDelete) {
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
    if (self.actionRowIndexPath) {
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.actionRowIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
    [self.tableView endUpdates];  
}

After you get the IndexPath you need to get the IndexPath.row and IndexPath.section and accordingly you need to add the object into your array at the desired index. 获取IndexPath之后,您需要获取IndexPath.rowIndexPath.section ,因此需要将对象添加到所需索引处的数组中。 For example: if you double tap the 2nd row in the 1st section then you need to add the object at index 4 of the array corresponding to the 1st section and then reload table. 例如:如果双击第一部分中的第二行,则需要在与第一部分相对应的数组的索引4处添加对象,然后重新加载表。 The cell would be added to the 3rd index of the 1st section. 该单元格将被添加到第一部分的第三索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM