gpt4 book ai didi

ios - UITableView 更新行选择数据

转载 作者:行者123 更新时间:2023-11-29 02:44:47 25 4
gpt4 key购买 nike

我有一个 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);
}
}];
}

问题是 namenewFileName 具有相同的值 name 并且这导致 NSFileManager 出错说文件已经存在。

我已经尝试删除 [nameField setText:name] 但问题仍然存在。

我运气不好,找不到问题,非常感谢您的帮助。

最佳答案

好吧,如果您已经知道方法 moveItemAtPath:toPath: 仅在旧文件名和新文件名相同的情况下才会导致错误,那么应该很容易捕获此错误:

if (![newFileName isEqualToString:name]) {
[fm moveItemAtPath:[NSString stringWithFormat:@"%@%@",directory,name] toPath:[NSString stringWithFormat:@"%@%@",directory,newFileName] error:&error];
}

现在,当新文件名与旧文件名不同时,您的文件只会被移动(即重命名)。


编辑:

此外,如果您想获取用户刚刚在警报 View 中输入的新文件名,您应该输入以下行:

NSString *newFileName = nameField.text;

在你的完成 block 中。否则,它将在首次显示警报 View 时设置,因此具有初始值。将它们放在一起:

[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: 不是 native UIAlertView 方法。已创建 Objective-C 类别以使用此方法扩展 UIAlertView。可以找到一个示例 here .

关于ios - UITableView 更新行选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25282943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com