iphone - UITableView 在选定行下方添加行

标签 iphone ios uitableview

我有一个工作 TableView ,我可以在其中双击一个单元格,它会在下面添加一个“操作”单元格,上面编程了四个按钮。 今天我将字母部分添加到 TableView 和部分索引,但我无法再使用此功能。 我已经在代码中添加了一系列 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.rowIndexPath.section,因此您需要将对象添加到您的数组位于所需的索引处。例如:如果您双击第 1 部分的第 2 行,则需要在与第 1 部分对应的数组的索引 4 处添加对象,然后重新加载表。该单元格将添加到第 1 部分的第 3 个索引。

关于iphone - UITableView 在选定行下方添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13411403/

相关文章:

ios - 以错误的顺序调用 UITableView cellForRowAtIndexPath

ios - 如果在 tableview iOS 中找不到数据,则无法在中心显示 View ?

iphone - NSLayoutConstraint错误: making full subview

objective-c - 在 iOS 中使用 clock() 函数获取 tics

iPhone WiFi 子网掩码和路由器地址

ios - 在 View Controller 之间设置 UIImageView.image

ios - 具有多个嵌入 segues 的 ContainerView

ios - TableView 中的动态节数和行数

iphone - NSDateFormatter、NSDate、UTC 和奇怪的日期格式

iphone - 无法为目标进程配置泄漏工具