如何在IOS中更新或刷新索引处的删除对象?在这种情况下
我创建了两个按钮“添加行”(UI)和“删除行”(编程)。如果我按“添加行”按钮以编程方式创建文本框,则“删除”按钮将进行迭代。当按“删除”按钮时,它将删除整行。请查看以下图像 第一个图像是关于默认显示编程创建的文本框、添加行(通过UI创建)和删除按钮 第二幅图是关于添加行按钮的。如果我按下添加行按钮文本框,删除按钮就会被迭代 第三幅图是关于手动将值输入文本框 第四个图像是关于。我在第一个文本框中输入值1,然后按delete按钮。然后删除整行 第五幅图像是关于在我从第四个链接或屏幕截图中按add row按钮后,第一个文本框应该是空的(我所期望的),但它填充了值1 我的一段代码用于在cellForRowAtIndexPath方法中创建删除按钮和文本框:如何在IOS中更新或刷新索引处的删除对象?在这种情况下,ios,uitableview,nsmutablearray,Ios,Uitableview,Nsmutablearray,我创建了两个按钮“添加行”(UI)和“删除行”(编程)。如果我按“添加行”按钮以编程方式创建文本框,则“删除”按钮将进行迭代。当按“删除”按钮时,它将删除整行。请查看以下图像 第一个图像是关于默认显示编程创建的文本框、添加行(通过UI创建)和删除按钮 第二幅图是关于添加行按钮的。如果我按下添加行按钮文本框,删除按钮就会被迭代 第三幅图是关于手动将值输入文本框 第四个图像是关于。我在第一个文本框中输入值1,然后按delete按钮。然后删除整行 第五幅图像是关于在我从第四个链接或屏幕截图中
- (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 row定义哪一行
- (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是可变数组。我发现addrow按钮没有问题。然后我尝试了一些在删除行操作中使用保留语法的方法。但是它不起作用。如何纠正这个问题。请提前感谢
删除行操作执行以下操作:
当我将记录器放在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”;在可变数组中。这是正确的解决方案还是其他选项 重用表视图中的单元格。考虑到这一点,在Cocoa的模式中,您应该确保这些单元格(模型视图控制器的“视图”)在用户键入值时有一些位置来存储这些值(“模型”),并且视图控制器(控制器)将在您添加、删除和更新行时促进模型的更新,以及确保
cellforrowatinexpath
根据模型中的值设置这七个文本字段的内容
但您的代码片段并没有考虑任何模型(或者,若这个self.Array
应该是模型,那个么您似乎并没有做任何事情来(a)在用户键入值时保存它们;或者(b)在调用cellForRowAtIndexPath
时用模型中的值加载单元格)。因此,当重复使用单元格时,这些文本字段中最后出现的任何值都可能出现在这些单元格中
举个例子,让我们假设我有一个cell prototype链接到一个cell子类,它有七个插座用于七个标签:
@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
//ViewController.m
#导入“ViewController.h”
#导入“CustomCell.h”
///电话号码模型对象
@接口电话号码:NSObject
@属性(非原子,强)NSString*digit1;
@属性(非原子,强)NSString*digit2;
@属性(非原子,强)NSString*digit3;
@属性(非原子,强)NSString*digit4;
@属性(非原子,强)NSString*digit5;
@属性(非原子,强)NSString*digit6;
@属性(非原子,强)NSString*digit7;
@结束
@实现电话号码
//这是故意空白的
@结束
@界面视图控制器()
///模型对象数组
@属性(非原子,强)NSMutableArray*PhoneNumber;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//初始化该数组
self.phoneNumbers=[NSMutableArray];
}
//返回模型对象数组中的项数
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回self.phoneNumbers.count;
}
//填充单元格时,请确保根据相应的模型对象填充七个文本字段
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
CustomCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
PhoneNumber*number=self.PhoneNumber[indexath.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;
返回单元;
}
//我将七个文本字段中的“编辑更改”操作连接到视图控制器中的这个方法
//哪个更新了模型
-(iAction)didEditingChangeTextField:(UITextField*)发送方{
CustomCell*单元格=(id)[[sender superview]superview];
NSAssert([cell isKindOfClass:[CustomCell class]],@“%@不是CustomCell”,发送方);
nsindepath*indepath=[self.tableView indexPathForCell:cell];
NSAssert(indepath,@“未找到indepath”);
PhoneNumber*PhoneNumber=self.PhoneNumber[indexath.row];
if(发送方==单元格.textField1)
phoneNumber.digit1=sender.text;
else if(发送方==单元格.textField2)
phoneNumber.digit2=sender.text;
else if(发送方==单元格.textField3)
phoneNumber.digit3=sender.text;
else if(发送方==单元格.textField4)
phoneNumber.digit4=sender.text;
else if(发送方==单元格.textField5)
phoneNumber.digit5=sender.text;
else if(发送方==单元格.textField6)
phoneNumber.digit6