如何更新或刷新 IOS 中索引处的删除对象?对于这种情况
How to update or refresh the Remove Object at index in IOS? For this scenario
我创建了两个按钮添加行(UI)和删除行(以编程方式)。如果我按添加行按钮以编程方式创建文本框和删除按钮得到 iterate.While 按删除按钮它删除整个 row.Please 看看下面的图片。
第一张图片是关于以编程方式创建的文本框、添加行(通过 UI 创建)和删除按钮的默认显示。
第二张图片是关于添加行的button.If我按下添加行按钮文本框和删除按钮得到迭代。
第三张图片是关于手动输入文本框的值。
第四张图片 about.I 在第一个文本框中输入值 1 并按删除 button.then 整行被删除。
第五张图片是关于在我按下第四张 link 或屏幕截图中的添加行按钮后,第一个文本框应该是空的(如我所料)。但它填充了值 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 执行整行删除,删除行定义了哪一行。
- (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 是可变的 array.I 发现添加行按钮没有问题。然后我尝试在删除行 action.But 中使用保留语法,它不会 works.How 来纠正这个 issue.Please,在此先感谢。
删除行操作执行以下操作:
当我将记录器放在 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;
}
)
之后删除操作需要place.Before,是否可以更改"2016-01-31" = 1; to "2016-01-31" = "";在可变的 array.I 中,这是正确的解决方案还是其他选择?
table 视图中的单元格被重复使用。考虑到这一点,在 Cocoa 的 Model-View-Controller 模式中,您应该确保这些单元格(Model-View-Controller 的 "view")有一些地方可以存储值作为用户键入它们("model")和视图控制器("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) 在删除行时从模型中删除条目。请注意,我不知道您的模型对象是什么,但由于它似乎有七个小文本字段,我认为它可能是 "phone number"。如果不是,请将该模型对象重命名为适合您的情况的名称。
// 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
我创建了两个按钮添加行(UI)和删除行(以编程方式)。如果我按添加行按钮以编程方式创建文本框和删除按钮得到 iterate.While 按删除按钮它删除整个 row.Please 看看下面的图片。
第一张图片是关于以编程方式创建的文本框、添加行(通过 UI 创建)和删除按钮的默认显示。
第二张图片是关于添加行的button.If我按下添加行按钮文本框和删除按钮得到迭代。
第三张图片是关于手动输入文本框的值。
第四张图片 about.I 在第一个文本框中输入值 1 并按删除 button.then 整行被删除。
第五张图片是关于在我按下第四张 link 或屏幕截图中的添加行按钮后,第一个文本框应该是空的(如我所料)。但它填充了值 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 执行整行删除,删除行定义了哪一行。
- (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 是可变的 array.I 发现添加行按钮没有问题。然后我尝试在删除行 action.But 中使用保留语法,它不会 works.How 来纠正这个 issue.Please,在此先感谢。
删除行操作执行以下操作:
当我将记录器放在 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;
}
)
之后删除操作需要place.Before,是否可以更改"2016-01-31" = 1; to "2016-01-31" = "";在可变的 array.I 中,这是正确的解决方案还是其他选择?
table 视图中的单元格被重复使用。考虑到这一点,在 Cocoa 的 Model-View-Controller 模式中,您应该确保这些单元格(Model-View-Controller 的 "view")有一些地方可以存储值作为用户键入它们("model")和视图控制器("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) 在删除行时从模型中删除条目。请注意,我不知道您的模型对象是什么,但由于它似乎有七个小文本字段,我认为它可能是 "phone number"。如果不是,请将该模型对象重命名为适合您的情况的名称。
// 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