UITableView在选定行下方添加行

时间:2012-11-16 06:12:42

标签: iphone ios uitableview

我有一个工作台视图,我可以双击一个单元格,它会添加一个“动作”单元格,下面有四个按钮编程。 今天我在tableview和section索引中添加了字母部分,我无法再使用此功能。 我已经在代码中添加了一系列NSLog来尝试找到问题而我不能,它似乎试图在同一部分中添加一行,并且比单击的单元格更向下一行,所以我'我不知道问题是什么。谁会知道我做错了什么? 如果有人能对此有所了解,我将非常感激。 (并且如果我的代码很麻烦或难以理解而道歉,我是新手,所以请随意提出我可以改进的内容!)

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

1 个答案:

答案 0 :(得分:1)

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