ios - 如何在表格 View 中编辑一行的文本内容?

标签 ios objective-c uitableview

我正在使用 Xcode 6。

假设我在 tableview 中显示了一个 Food 对象(每一行都是一个属性:名称、卡路里、蛋白质、维生素 C ......)。
我想让每一行内容都可以编辑。

一开始,我在想,在单击“编辑”按钮后,将行内容作为文本字段,但这个解决方案似乎很复杂。

现在,我想到,在选择一行之后,推送一个新的表格 View Controller ,其中只有一行包含一个标签(属性的名称,例如“卡路里”)和一个包含值的文本字段。
(其实我是受到 iPhone/iPod 中设置界面的启发,通用 > 关于 > 名称)

1-我可以通过将属性名称和值传递给它来使这个 TableView Controller 只有一行可重复用于我的食物对象的各种属性(名称、蛋白质、维生素C……),具体取决于选择的行?

2 - 如果是,这是实现我想要做的干净的方式吗?你有其他想法吗?

PS:我应该准确地说我的值可以是字符串(如名称属性)或数字(蛋白质,维生素 C,...)。

非常感谢你

enter image description here

最佳答案

我将向您展示我在我的一个应用程序中所做的事情。我们有一个配置文件部分,它是一个 tableViewController,其中包含很多用于用户配置文件数据的单元格。如果用户想要编辑该值,他或她必须单击单元格,我们将向他/她显示带有输入文本的警报以指定新值。

例如,如果您想在我们的应用中编辑您的描述:

enter image description here

enter image description here

所以你需要实现这些。

一些变量来存储您要编辑的单元格

@interface MyViewController (){

    //...

    NSString *actionSheetType; // to store what alert I have to show

    NSString *description; // In my example, to store my user desription

    //...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 捕捉点击的单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        //...

    } else if (indexPath.row == 2) {
        //...

    } else if (indexPath.row == 3) {
        //...

    } else if (indexPath.row == 4) {

        // Description

        actionSheetType = @"description"; // Set the type

        // Show the alert
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Edit description", nil) message:[[NSUserDefaults standardUserDefaults] stringForKey:USER_DESCRIPTION] delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
    } 

}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 获取新值
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Check what alert have you shown
    if ([actionSheetType isEqualToString:@"password"]){

    } else if ([actionSheetType isEqualToString:@"location"]){

    } else if ([actionSheetType isEqualToString:@"description"]){
        if (buttonIndex == 1) // if click on my alert button
        {
            // Edit my description
            description = [alertView textFieldAtIndex:0].text;
            // Any more actions you will need to do. For example, if you modify a cell or a table alement, reload the table
        }
    } else if ([actionSheetType isEqualToString:@"email"]){
        if (buttonIndex == 1)
        {
        }
    }
}

我希望这可以帮助你!如果您有任何疑问,请发表评论,我会尽快回复您:)

关于ios - 如何在表格 View 中编辑一行的文本内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29540221/

相关文章:

ios - 在 ScrollView 中从一个 TextInput 轻敲到另一个 TextInput 不应关闭键盘

objective-c - 解析带有特殊字符的 XML 文件

ios - 无法从呈现的 View Controller 访问委托(delegate)方法

ios - UITableView reloadData 自动调用resignFirstResponder

ios - 使用子类 UITableViewCell 的 scrollToRowAtIndexPath

由于 openldap 和 openssl makefile 错误,iOS 应用存档失败

iphone - 关于tableview绘图的一般理解

ios - Appium IOS SendKeys不起作用

ios - 如何将数据保存在 JSON 字典中并发送 Web 服务后调用?

ios - 带有尾随约束约束不稳定问题的 Objective-c 动画