gpt4 book ai didi

ios - 从 xcode 中单元格内的文本字段获取值

转载 作者:行者123 更新时间:2023-11-29 13:19:20 25 4
gpt4 key购买 nike

所以我有一个显示 3 个名字的弹出 TableView 。我想要得到的是,当用户从弹出表中选择一个名称时,它将关闭弹出窗口并在原始 View Controller 的标签或文本字段上显示该名称。这是我目前所拥有的:

(selectedName是我在原来的viewcontroller上的label的名字)

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedName.text = cell.textLabel.text;



}

它不起作用,我也在寻找一种方法,让弹出窗口在单击单元格时消失。有什么想法吗?

编辑:(设置单元格标签文本的代码)

cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] objectForKey:@"Name"];

最佳答案

您可以做的是定义一个协议(protocol),您的 tableview 可以使用该协议(protocol)与演示者进行通信。这是一种常见的做法。

这是一个粗略的例子:

@protocol MyTableViewDelegate<NSObject>
-(void)myTableView:(TheTableViewClass *)tableView didSelectValue:(NSString *)value;
@end

@interface TheTableViewClass : UITableViewController
@property (nonatomic, assign) id<MyTableViewDelegate> d;
@end


@implementation TheTableViewClass
@synthesize d;
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.d myTableView:self didSelectValue:cell.textLabel.text];
}
@end


@implementation MyPresenter
-(void)present
{
self.myTableViewClass = [MyTableViewClass ....];
self.myTableViewClass.d = self;
//present myTableViewClass...
}
-(void)myTableView:(TheTableViewClass *)tableView didSelectValue:(NSString *)value
{
//Set some label to the value passed in
//Dismiss self.myTablViewClass
}
@end

一些补充阅读:Working with Protocols

关于ios - 从 xcode 中单元格内的文本字段获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14779242/

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