简体   繁体   English

在原型表视图单元格中填充文本字段

[英]Populating a text field in a prototype Table View Cell

Apologies in advance, as I am extremely new to iOS and objective-C and this is probably a very basic question, but I've hit a wall, and after days of searching the Web, I'm still lost: 道歉提前,因为我非常新的iOS和Objective-C,这可能是一个很基本的问题,但我已经碰了壁,和Web搜索的天后,我还是输了:

I'm generating a dynamic table with various different types of prototype cells. 我正在生成具有各种不同类型的原型单元的动态表。 One type has only a text field into which the user can enter or edit an arbitrary string. 一种类型只有一个文本字段,用户可以在其中输入或编辑任意字符串。 I've designed the cells in the Storyboard Editor because I would rather not muck about with generating everything programmatically if possible. 我已经设计了Storyboard Editor中的单元,因为如果可能的话,我宁愿不以编程方式生成所有内容。

I have a string already in a variable which i want to use to populate the text field. 我已经在变量中使用了一个字符串,我想用它来填充文本字段。

How do you go about addressing and populating this field? 您如何处理和填充此字段? I assume the answer involves doing so at the moment the cell is called into existence with dequeueReusableCellWithIdentifier, but this is as far as I've managed to get...and I need help connecting the dots. 我以为答案是在通过dequeueReusableCellWithIdentifier调用细胞存在的那一刻这样做的,但是据我所知……我需要帮助连接点。

I have already read the Apple "Table View Programming Guide for iOS," and it's been unhelpful with this problem. 我已经阅读了Apple“ iOS的Table View编程指南”,它对于解决此问题没有帮助。

(if I could have tagged this question "noob"...) (如果我可以将此问题标记为“ noob” ...)

Create a new subclass of UITableViewCell and set the class of your cell to this class. 创建UITableViewCell的新子类并将您的单元格的类设置为此类。 (select the cell and then click the identity inspector, the 3rd icon from the left. Enter the class name there where it says: Class) You will also need to specify an Identifier for the cell. (选择单元格,然后单击身份检查器,即左侧的第3个图标。在其中输入类名称的地方输入:Class)您还需要为该单元格指定一个标识符。 I often times put in the same name as the class, so don't get the two confused in the code below. 我经常使用与该类相同的名称,因此不要在下面的代码中混淆两者。 One is the identifier, the other is a class. 一个是标识符,另一个是类。

In your cellForRowAtIndexPath, get an instance of your special class. 在cellForRowAtIndexPath中,获取特殊类的实例。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"EventCell";

    EventCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    [cell populateTextField];

    return cell;
}

When you have the storyboard open, you can control-drag from the text box to your new class to create an outlet property. 打开情节提要时,可以控制从文本框拖动到新类,以创建一个Outlet属性。 (Let's call it textField) (我们称之为textField)

Your property would look like this after you ctrl-drag and then enter when the dialog is shown: 按住ctrl并拖动后显示对话框时,您的属性将如下所示:

@property (weak, nonatomic) IBOutlet UITextField *textField;

Then in your new class, create that populateTextField method: 然后在新类中,创建该populateTextField方法:

-(void)populateTextField {
    self.textField.text = @"My special string";
}

Also note, that you can put in placeholder text on a textField, so you may not need to go through all of this. 还要注意,您可以在占位符文本上放置占位符文本,因此您可能不需要全部经历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM