1

我有一个工作表视图,我可以双击一个单元格,它会在下面添加一个“动作”单元格,上面有四个按钮。今天我将字母部分添加到表格视图和部分索引中,但我无法再使用此功能。我已经在代码中添加了一系列 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];  
}
4

1 回答 1

1

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

于 2012-11-16T06:59:14.747 回答