如何更新或刷新IOS索引中的删除对象?对于这种情况

时间:2016-02-11 11:05:17

标签: ios uitableview nsmutablearray

我创建了两个按钮添加行(UI)和删除行(以编程方式)。如果按下按行按钮添加行按钮,删除按钮获取迭代。按删除按钮删除整行。请看一下在图像下面。

enter image description here

第一张图片是关于以编程方式创建的文本框,添加行(通过UI创建)和删除按钮的默认显示。

enter image description here

第二个图像是关于添加行按钮。如果我按下添加行按钮文本框,删除按钮将被迭代。

enter image description here

第三张图片是关于手动输入文本框的值。

enter image description here

第四张图片是关于。我在第一个文本框中输入值1,然后按删除按钮。然后整行被删除。

enter image description here

第五张图片是关于我按第四个链接或屏幕截图中的添加行按钮后,第一个文本框应为空(我期待的是)。但它填充值为1。

我在cellForRowAtIndexPath方法中创建删除按钮和文本框的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// table view created via storyboard.
if (tableView == self.tblview)
{
// create a delete button dynamically     
UIButton *delete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
delete.frame = CGRectMake(525.0f, 5.0f, 75.0f, 30.0f);
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[cell addSubview:delete];
[delete addTarget:selfaction:@selector(deleterow:)forControlEvents:UIControlEventTouchUpInside];
[delete setTag:indexPath.row];

//dynamically created text fields.
UITextField *txtvalue =  [[UITextField alloc] init];
}
}

方法或操作删除行:此操作通过removeObjectAtIndex执行整行的删除,del行定义哪一行。

- (IBAction)deleterow:(id)sender
{
UIButton * delete = (UIButton *) sender;
// del row defines the which row of the delete tag has been pressed.
NSInteger delRow = delete.tag;
[self.Array removeObjectAtIndex:delRow];
[self.tblview reloadData];
}

操作添加行:

- (IBAction)addRow:(id)sender 
{
[self Data:[self.Array count]];
[self.tblview reloadData];
}

其中Array是可变数组。我发现添加行按钮没有问题。然后我尝试使用删除行动作中的保留语法。但它没有工作。如何纠正这个问题。请在此先感谢。

删除行操作执行以下操作:

当我将记录器放在self.array之前时,它会显示如下值:

(
{
"2016-01-31" = 1;
"2016-02-01" = "";
"2016-02-02" = "";
"2016-02-03" = "";
"2016-02-04" = "";
"2016-02-05" = "";
"2016-02-06" = "";
A = x;
b = x;
C = "";
D = "";
E = "";
F = "";
G = “RED";
H = 8;
}
) 

之后进行删除操作。在此之前,是否可以更改" 2016-01-31" = 1;到" 2016-01-31" ="&#34 ;;在可变数组中。它是正确的解决方案还是其他任何选项?

1 个答案:

答案 0 :(得分:0)

表视图中的单元格被重用。考虑到这一点,在Cocoa的Model-View-Controller模式中,您应该确保这些单元格(模型 - 视图 - 控制器的“视图”)有一些位置来存储值,因为用户键入它们(“模型”并且视图控制器(“控制器”)将在您添加,删除和更新行时促进模型的更新,并确保cellForRowAtIndexPath根据值设置这七个文本字段的内容在你的模型中。

但是你的代码片段没有考虑任何模型(或者,如果这个self.Array应该是模型,你似乎没有做任何事情(a)在用户输入值时保存值;(b)在调用cellForRowAtIndexPath时,用模型中的值加载单元格。因此,当重复使用单元格时,最后这些文本字段中的任何值都可能出现在这些单元格中。

举例来说,让我们想象一下,我的细胞原型链接到一个细胞亚类,它有七个标签的七个出口:

@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UITextField *textField3;
@property (weak, nonatomic) IBOutlet UITextField *textField4;
@property (weak, nonatomic) IBOutlet UITextField *textField5;
@property (weak, nonatomic) IBOutlet UITextField *textField6;
@property (weak, nonatomic) IBOutlet UITextField *textField7;

@end

@implementation CustomCell

// this is intentionally blank

@end

然后我的视图控制器(a)在添加行时填充模型结构; (b)在更新行时更新模型; (c)删除行时从模型中删除条目。注意,我不知道你的模型对象是什么,但由于它似乎有七个小文本字段,我认为它可能是一个“电话号码”。如果没有,请将该模型对象重命名为适合您案例的任何内容。

//  ViewController.m

#import "ViewController.h"
#import "CustomCell.h"

/// Phone number model object

@interface PhoneNumber : NSObject

@property (nonatomic, strong) NSString *digit1;
@property (nonatomic, strong) NSString *digit2;
@property (nonatomic, strong) NSString *digit3;
@property (nonatomic, strong) NSString *digit4;
@property (nonatomic, strong) NSString *digit5;
@property (nonatomic, strong) NSString *digit6;
@property (nonatomic, strong) NSString *digit7;
@end

@implementation PhoneNumber

// this is intentionally blank

@end


@interface ViewController ()

/// An array of model objects

@property (nonatomic, strong) NSMutableArray<PhoneNumber *> *phoneNumbers;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // initialize that array
    self.phoneNumbers = [NSMutableArray array];
}

// return the number of items in our array of model objects

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

// when I populate cell, make sure to populate the seven text fields on the basis of the corresponding model object

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    PhoneNumber *number = self.phoneNumbers[indexPath.row];

    cell.textField1.text = number.digit1;
    cell.textField2.text = number.digit2;
    cell.textField3.text = number.digit3;
    cell.textField4.text = number.digit4;
    cell.textField5.text = number.digit5;
    cell.textField6.text = number.digit6;
    cell.textField7.text = number.digit7;

    return cell;
}

// I hooked up "editing change" action in the seven text fields to this method in the view controller
// which updates the model

- (IBAction)didEditingChangeTextField:(UITextField *)sender {
    CustomCell *cell = (id)[[sender superview] superview];
    NSAssert([cell isKindOfClass:[CustomCell class]], @"%@ is not CustomCell", sender);

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSAssert(indexPath, @"did not find indexPath");

    PhoneNumber *phoneNumber = self.phoneNumbers[indexPath.row];

    if (sender == cell.textField1)
        phoneNumber.digit1 = sender.text;
    else if (sender == cell.textField2)
        phoneNumber.digit2 = sender.text;
    else if (sender == cell.textField3)
        phoneNumber.digit3 = sender.text;
    else if (sender == cell.textField4)
        phoneNumber.digit4 = sender.text;
    else if (sender == cell.textField5)
        phoneNumber.digit5 = sender.text;
    else if (sender == cell.textField6)
        phoneNumber.digit6 = sender.text;
    else if (sender == cell.textField7)
        phoneNumber.digit7 = sender.text;
}

// when I delete row, just delete from model object from array and then tell tableview to remove that row

- (IBAction)didTapDeleteButton:(UIButton *)sender {
    CustomCell *cell = (id)[[sender superview] superview];
    NSAssert([cell isKindOfClass:[CustomCell class]], @"%@ is not CustomCell", sender);

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSAssert(indexPath, @"did not find indexPath");

    [self.phoneNumbers removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}

// when I add row, just add to model collection and then tell table view to add that row

- (IBAction)didTapAddButton:(UIBarButtonItem *)sender {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.phoneNumbers.count inSection:0];
    [self.phoneNumbers addObject:[[PhoneNumber alloc] init]];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

@end