iphone - 在 UITableViewCell 中访问 UITextField

标签 iphone objective-c ios xcode

我正在使用一个 UITableView 和一个带有 UITextField 的子类原型(prototype)单元格。该单元在 4 个部分中总共使用了 4 次。这是它的代码:

...
- (void)viewDidLoad
{
[super viewDidLoad];
cellTitles = [[NSArray alloc] initWithObjects:@"Smell", @"Taste", @"Suits", @"Notes",  nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [cellTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cellTitles count] / [cellTitles count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [cellTitles objectAtIndex:section];
}  

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

AddInfoCell *addInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (addInfoCell == nil) {
    addInfoCell = [[AddInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
addInfoCell.cellTextView.delegate = self;

return addInfoCell;
}

我想将每个单元格中的文本添加到 dataSource 对象:

-(IBAction)nextButtonPressed:(id)sender {
...
[[dataSource objectAtIndex:dataSourceIndex] setObject:* forKey:@"Smellnote"];
[[dataSource objectAtIndex:dataSourceIndex] setObject:** forKey:@"Tastenote"];
//etc
…
}

*单元格 1 中的文本

**单元格 2 中的文本

如何访问我想要的文本?

最佳答案

也许你看错了。为什么不让 textField 的委托(delegate)成为单元格本身,即在 Cell 的类中,在 init... 方法中将委托(delegate)设置为 File'sOwner 或 self。

创建对数据源的引用并将数据源连同字典的@"Key"一起传递给您的单元格。

cell.dataSource = self.dictionary;
cell.dataKey = @"Tastenote";
//in cell .m
//textFieldDelegate method...
[self.dataSource setObject:self.cellTextView.text forKey:self.dataKey];

关于iphone - 在 UITableViewCell 中访问 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12698170/

相关文章:

javascript - SliderIOS – 自定义可移动图像

iphone - 与 APNS 服务器交互以向 iOS 设备发送推送通知的最简单方法是什么?

ios - 如何获取子类中UIButton的默认突出显示颜色/状态?

ios - 来自 UIAlertController 的键盘延迟关闭

IOS UINavigationController、pushViewController 不工作

ios - iOS 上的 UIDevice Battery State 在电池状态更改时不会更新

ios - 创建一组相同的 UIImageView subview

ios - 从 UIDocumentInteractionController 保存到 iCloud Drive 后切换回应用程序会删除导航层次结构

iphone - 在 iPhone 后台下载

ios - 在openGL ES中检测距相机最近的顶点