从视图传回值
大家好,我有一个关于编辑表格单元格的值并将编辑后的值返回到原始单元格的问题。
我有一个 UITableView,其中包含 5 个部分,每个部分 1 行。在 cellForRowAtIndexPath 中,我有以下内容:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSString *fieldTitle;
switch (indexPath.section)
{
case 0:
fieldTitle = @"First Name";
break;
case 1:
fieldTitle = @"Last Name";
break;
case 2:
fieldTitle = @"Company";
break;
case 3:
fieldTitle = @"Email Address";
break;
case 4:
fieldTitle = @"Password";
break;
}
cell.textLabel.text = fieldTitle;
单击一行时,将触发 didSelectRowAtIndexPath,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
AddField *addField = [[AddField alloc] initWithStyle:UITableViewStyleGrouped];
addField.field = cell.textLabel.text;
addField.title = cell.textLabel.text;
addField.value = cell.detailTextLabel.text;
[self.navigationController pushViewController:addField animated:YES];
}
这将设置另一个包含一个部分和一行的 UITableView。该表使用我编写的自定义单元格,其中包含用户可以编辑的文本字段。有一个完成按钮,用户可以单击以返回到上一个视图。
我的问题是:如何获取用户在 AddField 视图中输入的值,以显示在上一个视图中所选表格单元格的详细文本标签中?此功能与在 iPhone 的本机日历应用程序中添加新事件的标题非常相似。
感谢您为我提供的任何帮助,如果您需要更多信息,请告诉我。
Hey everybody, I had a question about editing the value of a table cell and returning the edited value back to the original cell.
I have a UITableView which contains 5 sections of 1 row each. Within cellForRowAtIndexPath I have the following:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSString *fieldTitle;
switch (indexPath.section)
{
case 0:
fieldTitle = @"First Name";
break;
case 1:
fieldTitle = @"Last Name";
break;
case 2:
fieldTitle = @"Company";
break;
case 3:
fieldTitle = @"Email Address";
break;
case 4:
fieldTitle = @"Password";
break;
}
cell.textLabel.text = fieldTitle;
When a row is clicked, didSelectRowAtIndexPath is fired as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
AddField *addField = [[AddField alloc] initWithStyle:UITableViewStyleGrouped];
addField.field = cell.textLabel.text;
addField.title = cell.textLabel.text;
addField.value = cell.detailTextLabel.text;
[self.navigationController pushViewController:addField animated:YES];
}
This sets up another UITableView that contains one section and one row. This table utilizes a custom cell I wrote that contains a text field the user can edit. There is a done button the user can click to go back to the previous view.
My question is this: How do I get the value the user entered in the AddField view to appear in the detailText label on the selected table cell in the previous view? This functionality would be very similar to adding the title of a new event in the iPhone's native calendar application.
Thanks for any help I can get, and let me know if you need more information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过在原始 tableView 上调用 reloadData ?当然,您需要更改代码,以便 fieldTitle 从 NSStrings 数组中获取其值......并将 tableview 数据源设置为该数组......
Have you tried calling reloadData on the original tableView? Of course you would need chnage your code so that fieldTitle to get its values from an array of NSStrings......and set the tableview datasource to that array....
为此,您可以使用委托。声明一个作为您的单元委托的协议,如下所示(根据记忆,如果无法编译,抱歉):
向您的自定义单元类添加一个“id 委托”,然后添加一个属性(非原子、分配)并综合它。
然后,当您在 didSelectRowAtIndexPath 中设置单元格时,您将执行以下操作:
现在,在视图控制器中,您只需实现该协议 (CellDelegate),实现方法 fieldValueChanged...,这将由单元格调用。
现在,在单元格中,当值更改时,只需调用 [delegate fieldValueChanged:addfield];
注意:实际上,您应该在您编写的自定义单元格类中设置 AddField ...
You would use delegates for this. Declare a protocol which is your cell delegate as follows (from memory so apologies if it doesn't compile):
Add an "id delegate" to your custom cell class, and add a property (nonatomic, assign) and synthesise it.
Then when you setup your cell in didSelectRowAtIndexPath you do the following:
Now in you view controller you simply implement that protocol (CellDelegate), implement the method fieldValueChanged.... this will get called by the cell.
Now in the cell when the value is changed simply call [delegate fieldValueChanged:addfield];
NOTE: really you should be setting up the AddField WITHIN the custom cell class you write...