UITableView 在所选行下方添加行



我有一个工作表视图,我可以双击一个单元格,它会添加一个"动作"单元格,下面编程了四个按钮。今天,我在表格视图中添加了字母部分和部分索引,但我无法再使用此功能。我已经在代码中添加了一整套 NSLogs 以尝试找到问题,但我不能,它似乎试图在同一部分中添加一行,并且比点击的单元格更靠下一行,所以我不确定问题是什么。有人知道我做错了什么吗?如果有人能对此有所了解,我将不胜感激。(如果我的代码很麻烦或难以遵循,我很抱歉,我是新手,所以请随时建议我可以改进的地方!

- (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];  
}
获取 IndexPath

后,您需要获取 IndexPath.row 和 IndexPath.section,因此您需要将对象添加到所需索引处的数组中。例如:如果双击第一部分中的第二行,则需要在对应于第一部分的数组的索引 4 处添加对象,然后重新加载表。该单元格将添加到第 1 部分的第 3 个索引中。

最新更新