Ios UITableView更新行选择上的数据
我有一个Ios UITableView更新行选择上的数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableView,它从数组中获取数据,数组包含目录的文件名 我正在尝试让用户在选择行时编辑文件名 我的代码是: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self editChattWithName:[self.listArray objectAtIndex:indexPath.row] atIndex:indexPath];
UITableView
,它从数组中获取数据,数组包含目录的文件名
我正在尝试让用户在选择行时编辑文件名
我的代码是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self editChattWithName:[self.listArray objectAtIndex:indexPath.row] atIndex:indexPath];
[self.tabView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)editChattWithName:(NSString*)name atIndex:(NSIndexPath *)indexPath {
UIAlertView* editAlert = [[UIAlertView alloc]
initWithTitle:nil
message:@"Edit FileName"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Update", nil];
[editAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField* nameField = [editAlert textFieldAtIndex:0];
[nameField setPlaceholder:@"New FileName"];
[nameField setText:name];
[editAlert show];
[editAlert release];
NSString *newFileName = nameField.text;
[editAlert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 0) { }
else if (buttonIndex == 1) {
NSError *error;
// Edit filename inside directory
[fm moveItemAtPath:[NSString stringWithFormat:@"%@%@",directory,name] toPath:[NSString stringWithFormat:@"%@%@",directory,newFileName] error:&error];
// Update value inside array
[self.listArray replaceObjectAtIndex:indexPath.row withObject:newChatName];
// reload table data to show new filename
[self.tabView reloadData];
NSLog(@"Old Filename: %@%@",directory,name);
NSLog(@"New Filename: %@%@",directory,newFileName);
NSLog(@"Error: %@",error);
}
}];
}
问题是name
和newFileName
具有相同的值name
,这导致NSFileManager出现错误,表示该文件已存在
我已尝试删除[nameField setText:name]
,但问题仍然存在
我运气不好,无法找到问题,非常感谢您的帮助。好吧,如果您已经知道方法
moveItemAtPath:toPath:
仅在新旧文件名相同的情况下才会导致错误,那么应该很容易捕获此错误:
if (![newFileName isEqualToString:name]) {
[fm moveItemAtPath:[NSString stringWithFormat:@"%@%@",directory,name] toPath:[NSString stringWithFormat:@"%@%@",directory,newFileName] error:&error];
}
现在,当新文件名与旧文件名不同时,您的文件将仅被移动(即重命名)
编辑: 此外,如果要获取用户刚刚在“警报”视图中输入的新文件名,则应在下面这一行:
NSString *newFileName = nameField.text;
在你的完成区。否则,它将在首次显示警报视图时设置,因此具有其初始值。总而言之:
[editAlert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 1) {
NSString *newFileName = nameField.text;
NSError *error;
// Edit filename inside directory
if (![newFileName isEqualToString:name]) {
[fm moveItemAtPath:[NSString stringWithFormat:@"%@%@",directory,name] toPath:[NSString stringWithFormat:@"%@%@",directory,newFileName] error:&error];
}
// Update value inside array
[self.listArray replaceObjectAtIndex:indexPath.row withObject:newChatName];
// reload table data to show new filename
[self.tabView reloadData];
}
}];
补充:
为避免混淆其他用户,应注意
showWithCompletion:
不是本机UIAlertView
方法。已创建Objective-C类别,以使用此方法扩展UIAlertView
。可以找到一个示例。replaceObjectAtIndex:withObject:
方法不是问题所在。问题(根据日志)是newFileName
与name
具有相同的字符串,这导致了NSFileManager
错误:文件已经存在。名称不应相同,我在文本字段中写入了不同的字符串。从我所看到的replaceObjectAtIndex:withObject:
是您调用的唯一NSFileManager
方法,因此我看不出NSFileManager
如果不是此方法,为什么会抛出错误。您是否尝试过插入if语句?特别是因为您在问题中写道:我尝试过删除[nameField setText:name],但问题仍然存在。因此,显然错误与设置名称字段的文本无关。replaceObjectAtIndex:withObject:
不是NSFileManager方法,而是moveItemAtPath:toPath:
方法。我想你不明白我想做什么。我希望当用户单击某个单元格时,会显示一个带有UITextField的警报,以编辑该单元格。通过编辑单元格,我必须在目录中重命名该文件,并在NSArray
中更改其值,然后重新加载数据。因此,基本上添加if语句将返回false,因为主要问题是UITextField textnewFileName
出于某种原因获得与name
相同的字符串。你是对的,对不起。我只是在回复和评论中复制了错误的方法名称。当然,我指的是NSFileManager
方法moveItemAtPath:toPath:
。不过,我建议的代码包含了正确的方法名称。我已经更新了我的答案,更正了方法名,并添加了另一个获取新文件名的建议。