删除行时无法刷新UITableView

时间:2014-05-21 05:44:43

标签: ios objective-c uitableview

我有UITableView个包含UITextFields的行。用户将数据输入这些字段。用户可以删除或添加任何行。当我试图删除任何行时,它会删除数组中的正确行(包含所有单元格的引用),但UITextfield始终显示最后一行被删除。

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _countEmailValues.count ;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CustomEmailCell";

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[LACustomEmailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.emailTextField.tag = 555;
    cell.deleteEmailFieldButton.tag = indexPath.row;
    NSLog(@"delete email Field tag %i",cell.deleteEmailFieldButton.tag );
    cell.emailTextField.delegate = self;

    if ([_countEmailValues count] > 1  )
    {
        cell.deleteEmailFieldButton.hidden = false;
    }
    else
    {
         cell.deleteEmailFieldButton.hidden = true;
    }

    // Reason why I am adding cell.emailtextfield in this delegate? is should be in addButtonclick but cell.emailtextfield is not
    // initialized there. Also adding of only cell will give null for the emailTextField.
    // So adding all cells here and then removing duplicates entires and accessing over the app.

    [_emailValues addObject:cell.emailTextField];

    // Remove Duplicate elements.
    NSArray *emailFieldCollection  = [_emailValues copy];
    NSInteger index = [emailFieldCollection count ] -1;

    for (id object in [emailFieldCollection reverseObjectEnumerator])
    {
        if ([_emailValues indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound)
        {
            [_emailValues removeObjectAtIndex:index];
        }

        index--;
    }

    NSLog(@"Row : %i",indexPath.row);
    return cell;
}

- (IBAction)deleteEmailClick:(UIButton *)sender
{
   NSIndexPath *index =  self.emailTableView.indexPathForSelectedRow;
   // [self.emailTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] animated:YES scrollPosition:0];

    NSIndexPath *indexPath = [_emailTableView indexPathForSelectedRow];
    NSLog(@"Tags %i", sender.tag);

//    [self.emailTableView reloadData];

    if ([_countEmailValues count] > 0)
    {
        [  _countEmailValues  removeObjectAtIndex:sender.tag];
    }
      //  NSLog(@"array after %@",_countEmailValues);
    if ([_countEmailValues count] == 0)
    {
       // _deleteEmailButton.hidden = true;
        _doneButton.hidden = true;
    }

    NSLog(@"array before %@",_emailValues);
    if ([_emailValues count] > 0)
    {
        [_emailValues removeObjectAtIndex:sender.tag];
    }

    [self.emailTableView reloadData];
    }

_emailValues正确更新,但字段中的数据始终清除最后一个。 EX:在图像中如果我删除“b”,则_emailValues被正确清除但是ui显示两个具有数据“a”&的字段。 “b”。我错过了什么? 请帮助。 enter image description here

1 个答案:

答案 0 :(得分:0)

您没有删除该单元格。在deleteEmailClick添加此行

[self.emailTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

另外,我想指出使用块实现删除的另一种方法。这将确保您始终拥有正确的单元格,而无需标记删除按钮。这将更清洁和可维护。在您的自定义单元格类LACustomEmailCell中声明一个块属性,如此

@property (strong, nonatomic) void (^deleteButtonTappedBlock)();

LACustomEmailCell课程中连接删除按钮的IBAction,并从那里调用此块。我假设名称为deleteButtonPressed

- (IBAction)deleteButtonPressed:(id)sender {
    self.deleteButtonTappedBlock();
}

现在在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您可以像这样设置块

[cell setDeleteButtonTappedBlock:^(){
        //Action to perform when cell is deleted. In your case the code in `deleteEmailClick` will go here.
       //In the end delete the cell.
        [self.emailTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];

我希望这有帮助