【问题标题】:How to save data from a textfield to a tableview?如何将文本字段中的数据保存到表格视图?
【发布时间】:2013-10-12 05:33:45
【问题描述】:

我正在开发我的第一个应用程序,而且我是 Objective-C 的新手,我想做它,所以当有人输入 text field 然后按下按钮时,它会将其保存到 table view .有谁知道如何做到这一点或知道任何教程。

【问题讨论】:

    标签: ios objective-c iphone tableview textfield


    【解决方案1】:

    您所要做的就是每次按下按钮时都会刷新/更新您的表格视图。

    - (IBAction)buttonPressed:(id)sender {
    NSString *textNameToAdd = yourTextField.text;
    [containerArrayForTableView addObject:textNameToAdd];
    [myTableView reloadData];
    }
    
    // UITABLEVIEW DELEGATES
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
     }
    
     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    // Usually the number of items in your array (the one that holds your list)
    return [containerArrayForTableView count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell... setting the text of our cell's label
    cell.textLabel.text = [containerArrayForTableView objectAtIndex:indexPath.row];
    return cell;
    }
    

    如果您在使用 UITableView 配置时遇到问题,请参考 here

    【讨论】:

    • 非常感谢,但是当我把它放在我的视图控制器中时,我真的很新
    • 去你的 .h 文件添加 UITableViewDelegate,像这样.. @interface ViewController : UIViewController
    • 确保你的 UITableViewDelegates 和你的 IBAction 也用于 UIButton。顺便说一句,如果这对您的问题有帮助,请投票给我的答案..
    • 检查我上面提到的链接,它非常简单直接。
    【解决方案2】:

    【讨论】:

    • 有什么办法可以使用多个文本字段,并且文本字段与表格视图位于不同的视图上
    • @JacobBanks,检查新的演示。
    • @JacobBanks, BaseViewController 是我们用于常用方法的类。请注意。
    • 非常感谢。但由于我有点新,我不知道很多关于 xibs 的方法,你可以在情节提要中制作它。在我的应用程序中,我正在使用情节提要
    • @JacobBanks,storyboard.xib 文件无关紧要。这只是逻辑。在这两种情况下都是一样的。只是推送和弹出页面是不同的。相信我,当您为某些客户开发应用程序时,您不会使用storyboard。因为使用它会太复杂。但是,如果您使用 .xib 文件,那么事情对您来说会容易得多。所以我认为我的回答是绝对正确的。
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2015-07-05
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多