Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用UI按钮清除UITableView中的所有UITextField更改_Iphone_Xcode_Uitableview_Uitextfield - Fatal编程技术网

Iphone 使用UI按钮清除UITableView中的所有UITextField更改

Iphone 使用UI按钮清除UITableView中的所有UITextField更改,iphone,xcode,uitableview,uitextfield,Iphone,Xcode,Uitableview,Uitextfield,我有一个UITableView,有十几行,每行包含一个UITextField。 默认情况下,如果用户以前未编辑文本字段,UITextField包含占位符值“Add value”: UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(158, 6, 148, 24)]; NSString *strReplacement = [valueArray objectAtIndex:indexPath.row];

我有一个UITableView,有十几行,每行包含一个UITextField。 默认情况下,如果用户以前未编辑文本字段,UITextField包含占位符值“Add value”:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(158, 6, 148, 24)];

NSString *strReplacement = [valueArray objectAtIndex:indexPath.row];

if (([strReplacement length] != 0) {
    textField.text = strReplacement;
} else {
    textField.placeholder = @"Add Value";
}

textField.delegate = self;
[cell addSubview:textField];
[textField release];
到目前为止还不错

我还向UITableView的页脚添加了一个UIButton。 我想要的是,当用户单击UITableView按钮时,清除所有编辑的值并刷新UITableView中的所有UITextFields

我可以很容易地从valueArray中删除所有对象,但我不知道如何刷新所有UITableView单元格以反映更改

感谢您的帮助。
lq

我相信你要找的是

[tableView reloadData];

我相信你要找的是

[tableView reloadData];

Filipe的解决方案也应该起作用,但是,应该尽可能避免调用
reloadData
,因为调用此方法会带来很高的性能开销

您需要一个类,该类既引用UIButton实例,也引用屏幕/表中的UIExtField的所有实例。听起来像是UITableView控制器子类的完美工作

在上面的代码中,为什么不将创建的每个UITextField添加到UITableView控制器中的一组文本字段中?然后,当用户按下UIButton时,该操作可以调用控制器类中的某个方法,该方法通过NSArray中的所有UITextField元素循环,将每个实例的
文本
属性设置为
@“


警告:如果您正在重用单元格,则可能必须确保控制器的UITextFields NSArray得到正确更新。

Filipe的解决方案也应该可以工作,但是,应尽可能避免调用
重新加载数据
,因为调用此方法会带来很高的性能开销

您需要一个类,该类既引用UIButton实例,也引用屏幕/表中的UIExtField的所有实例。听起来像是UITableView控制器子类的完美工作

在上面的代码中,为什么不将创建的每个UITextField添加到UITableView控制器中的一组文本字段中?然后,当用户按下UIButton时,该操作可以调用控制器类中的某个方法,该方法通过NSArray中的所有UITextField元素循环,将每个实例的
文本
属性设置为
@“


警告:如果您正在重用单元格,则可能必须确保控制器的UITextFields NSArray已正确更新。

经过几个小时的反复试验,我提出了此解决方案。UIButton“全部清除”调用以下方法:

- (IBAction)clearValues:(id)sender {

    // count the number of values in the array (this is the same as the number of rows in the table)
    int count = [valueArray count];

    // remove all values from the array (deletes any user added values):
    [self.valueArray removeAllObjects];

    UITableViewCell *cell;
    UITextField *textField;

    // loop through each row in the table and put nil in each UITextField:
    for (NSUInteger i = 0; i < count; ++i) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        cell = [self.wordsTableView cellForRowAtIndexPath:indexPath];

        // NOTE: Make sure none of your tags are set to 0 since all non-tagged objects are zero.
        // In table construction, your textFieldTags should be: textField.tag=indexPath.row+1;

        textField = (UITextField*)[cell viewWithTag:i+1];
        textField.text = nil;

    }

}
-(iAction)clearValues:(id)发送方{
//计算数组中的值数(这与表中的行数相同)
int count=[valueArray count];
//从数组中删除所有值(删除任何用户添加的值):
[self.valueArray removeAllObjects];
UITableViewCell*单元格;
UITextField*textField;
//循环表中的每一行,并在每个UITextField中输入nil:
对于(整数i=0;i
经过几个小时的反复试验,我想出了这个解决方案。UIButton“全部清除”调用以下方法:

- (IBAction)clearValues:(id)sender {

    // count the number of values in the array (this is the same as the number of rows in the table)
    int count = [valueArray count];

    // remove all values from the array (deletes any user added values):
    [self.valueArray removeAllObjects];

    UITableViewCell *cell;
    UITextField *textField;

    // loop through each row in the table and put nil in each UITextField:
    for (NSUInteger i = 0; i < count; ++i) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        cell = [self.wordsTableView cellForRowAtIndexPath:indexPath];

        // NOTE: Make sure none of your tags are set to 0 since all non-tagged objects are zero.
        // In table construction, your textFieldTags should be: textField.tag=indexPath.row+1;

        textField = (UITextField*)[cell viewWithTag:i+1];
        textField.text = nil;

    }

}
-(iAction)clearValues:(id)发送方{
//计算数组中的值数(这与表中的行数相同)
int count=[valueArray count];
//从数组中删除所有值(删除任何用户添加的值):
[self.valueArray removeAllObjects];
UITableViewCell*单元格;
UITextField*textField;
//循环表中的每一行,并在每个UITextField中输入nil:
对于(整数i=0;i
您的解决方案感觉很奇怪。Filipe认为正确的方法是使用
[wordsTableView reloadData]
,这将导致对每个可见单元格调用
tableView:cellforrowatinexpath:
。当您在表格中滚动时,也会调用该方法,因此,如果重新加载数据不起作用,那么在更改和滚动数据时,可能还会出现数据更新不正确的错误。在clearValues方法中,通过调用
tableView:CellForRowatineXpath:
执行相同的操作

我认为真正的问题在于您的
tableView:cellforrowatinexpath:
实现。该方法通常有两个部分。首先,创建或循环使用一个单元格以获取引用,如下所示:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
其中,if语句通常是您应该向单元格添加子视图的唯一位置。如果
dequeueReusableCellWithIdentifier:
返回一个单元格,则它应该已经具有子视图

然后,在if语句之后,填充或更新子视图的内容。原始代码的问题在于,它填充文本字段并将其添加为子视图,假设单元格中还没有文本字段。因此,您的
表视图:cellforrowatinexp