【问题标题】:iPhone - get values of UITextField which are placed in customized UITableViewCelliPhone - 获取放置在自定义 UITableViewCell 中的 UITextField 的值
【发布时间】:2011-02-12 03:28:58
【问题描述】:

通常我有标签和一些文本字段,它们是 IBOutlets,如果有人单击按钮,我可以轻松读取这些文本字段的文本值

self.myTextfield1.text;

但现在我有一个带有自定义表格单元格的UITableView。在这些单元格中,我有一个标签和一个文本字段。加载时,我在cellForRowAtIndexPath 中执行此操作:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = (MyCell*)[theTableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (!cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            cell = self.myCell;
            self.myCell = nil;
    }
    switch (indexPath.section) {
    case 0:
        switch (indexPath.row) {
            case 0:
                cell.myCellLabel.text = @"Label1";
                cell.myCellTextField.text = @"...";
                break;
            case 1:
                cell.myCellLabel.text = @"Label2";
                cell.myCellTextField.text = @"...";
                break;
            // and so on...
            default:
                break;
        }
        break;
    case 1:
        switch (indexPath.row) {
            case 0:
                cell.myCellLabel.text = @"Label1_1";
                cell.myCellTextField.text = @"...";
                break;
            // and so on...
        }
    default:
        break;
    }
    return cell;
}

因此,如果有人单击该按钮,我将无法访问带有self.mytextfield1.text 的字段的文本值。如何获取字段的值?我知道标签的值应该在哪个部分和哪一行。因此,如果我可以结合节和行号获得文本字段的值,那就太好了。

也许有人知道如何解决这个问题?

最好的问候,蒂姆。

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    如果将文本字段的值存储在数组中,则数组的索引将与行相同。你甚至可以有多个数组: cellLabelArray cellTextFieldArray 这不是非常优雅,但可以解决问题。然后你会摆脱你的 switch 语句并做这样的事情:

    cell.myCellLabel.text = [cellLabelArray objectAtIndex:indexPath:row];
    cell.myTextField.text = [cellTextFieldArray objectAtIndex:indexPath:row];
    

    使用数组作为 UITableView 的数据源是一种常见的模式。

    我不知道按下按钮时会发生什么,但如果您需要更改标签的值,您只需更改数组中的值,然后向 tableview 发送reloadData 消息。

    【讨论】:

      【解决方案2】:

      这里的问题是表格单元格被重复使用,因此您不能从单元格中“拉”输入的值,而是让单元格使用 UITextFieldDelegate 将输入的值“推送”给您。

      在自定义 UITableViewCell 中,为部分和行添加变量,将单元格设置为文本字段委托,并使用自定义委托协议将生成的文本字段事件转发到带有附加部分和行的视图控制器。然后让 viewcontroller 将传入的数据写入 UITableView 数据源读取数据的同一位置。

      这是一个没有部分但带有“类型”的示例:

      #import <Foundation/Foundation.h>
      
      @protocol ArticleTextFieldViewCellDelegate
      
      -(void)didUpdateTextField:(int)index type:(int)type withText:(NSString*)text;
      
      @end
      
      @interface ArticleTextFieldViewCell : UITableViewCell <UITextFieldDelegate> {
      
          IBOutlet UITextField* valueTextField;
      
          id<ArticleTextFieldViewCellDelegate> delegate;
      
          int index;
          int type;
      }
      
      @property (nonatomic, retain) IBOutlet UITextField* valueTextField;
      @property (nonatomic, retain) id<ArticleTextFieldViewCellDelegate> delegate;
      @property (nonatomic, assign) int index;
      @property (nonatomic, assign) int type;
      
      @end
      

      和实施

      #import "ArticleTextFieldViewCell.h"
      #import <UIKit/UIKit.h>
      
      @implementation ArticleTextFieldViewCell
      
      @synthesize type, valueTextField, delegate, index;
      
      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
          if(delegate) {
              [delegate didUpdateTextField:index type:type withText:[textField.text stringByReplacingCharactersInRange:range withString:string]];
          }
          return YES;
      }
      
      - (BOOL)textFieldShouldReturn:(UITextField *)textField {
          [textField resignFirstResponder];
      
          return YES;
      }
      
      - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
          return YES;
      }
      
      - (void)textFieldDidEndEditing:(UITextField *)textField {
          // optionally trigger delegate method here
      }
      
      @end
      

      如果每个单元格有多个文本字段,请使用更多委托方法或附加变量,例如“类型”。

      【讨论】:

      • 我喜欢这个组织,它比尝试从文本字段中提取值更有意义。但是,如果您的表可以重新排序和删除行,那么您必须小心存储在自定义单元格中的行和节值的一致性。此外,您应该强调需要为您的解决方案创建自定义单元格类,OP 可能没有,您无需创建一个即可使用特定的 loading-custom-cell-from-nib 模式。
      • 根据 Walter 的响应,您的数据源也需要一些调整。如果需要多个部分和行,请使用数组数组。
      猜你喜欢
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2013-10-24
      相关资源
      最近更新 更多