将文本字段数据保存在 xcode 中的可扩展表格单元格中

Posted

技术标签:

【中文标题】将文本字段数据保存在 xcode 中的可扩展表格单元格中【英文标题】:saving text field data inside a expandable table cell in xcode 【发布时间】:2014-04-23 07:43:23 【问题描述】:

我是 ios 编程新手,我想创建一个可扩展的表格单元格,里面有一个文本字段。我必须唯一地保存在每个单元格中输入的文本,但是当我在文本字段中输入一些文本时移动到另一个单元格,文本被删除。我也在这里发布了我的代码。

帮我解决这个问题 提前致谢

viewcontroller.h

#import <UIKit/UIKit.h>

@interface CNViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
int selectedIndex;
NSMutableArray *title;
UITextField *comments;


@end

viewcontroller.m

#import "CNViewController.h"
#import "CNExapndingTableViewCell.h"

@interface CNViewController ()

@property (nonatomic,strong) IBOutlet UITableView *tableView;

@end

@implementation CNViewController

- (void)viewDidLoad

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate=self;
self.tableView.dataSource=self;

//Set Index to -1 sating no cell is expanded or should Expand

selectedIndex = -1;
title=[[NSMutableArray alloc]init];
NSString *string;
for(int ii=1;ii<=10;ii++)
    string = [[NSString alloc]initWithFormat:@"Row %i ",ii];
    [title addObject:string];


    //NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    //self.textView = [userdf objectForKey:@"yeon"];





-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return title.count;


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = @"expandingCell";
CNExapndingTableViewCell *cell=(CNExapndingTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"ExpandingCell" owner:self options:nil];
    cell=[nib objectAtIndex:0];

if(selectedIndex==indexPath.row)
    //Do Expanded cell Stuff

   [cell.textView setHidden:NO];
    cell.textView.delegate=self;
    cell.textView.tag=indexPath.row;


else
    //Do Closed Cell Stuff
    [cell.textView setHidden:YES];


cell.titleLabel.text=[title objectAtIndex:indexPath.row];


return cell;


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
if(selectedIndex==indexPath.row)
    return 120;
else
    return 44;



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//User Taps Expanded row
if(selectedIndex==indexPath.row)
    selectedIndex=-1;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    return;

//user taps on different row
if(selectedIndex!=indexPath.row)
    NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:0];
   selectedIndex=indexPath.row;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];

    

//user taps on new row if none is selected

selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

return;


@end

细胞.h

#import <UIKit/UIKit.h>

@interface CNExapndingTableViewCell : UITableViewCell



@property (nonatomic,strong) IBOutlet UILabel *titleLabel;

@property (nonatomic,readwrite) IBOutlet UITextField *textView;

@end

细胞.m

#import "CNExapndingTableViewCell.h"

@implementation CNExapndingTableViewCell

@synthesize titleLabel,textView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    // Initialization code
    
return self;


- (void)awakeFromNib

// Initialization code


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

[super setSelected:selected animated:animated];

// Configure the view for the selected state


@end

【问题讨论】:

您需要保存文本,然后在 cellForRowAtIndexPath 中您需要将 textviews 文本设置回保存的文本。 【参考方案1】:

我的做法是这样的:

创建一个 TableViewController 的单例实例,在其中放置一个存储标签内容的数组。

@interFace TableViewController : UIViewController
    @property (nonatomic,retain) NSMutableArray * labelArr;
    + (id)instance;
@end


static TableViewController *TableViewControllerInstance;
@ implementation TableViewController
+ (id)instance 
    static TableViewController *TableViewControllerInstance = nil;
    @synchronized(self) 
        if (TableViewControllerInstance == nil)
            TableViewControllerInstance = [[self alloc] init];
    
    return TableViewControllerInstance;


-(id)init
    if (self==[super init])
        self.labelArr = [[NSMutableArray alloc] init];
        for (int i=0;i<listLength;i++)
            [self.labelArr addobject:[NSString stringWithFormat:@""];
    
    return self;



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 


    CNExapndingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) 
        cell = [[[CNExapndingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
    

    [cell.textView setText: [self.labelArr objectAtIndex:indexPath.row]];
    cell.tag = indexpath.row;
    return cell;


@end

然后在 CNExapndingTableViewCell 中检查 UITextField 值何时发生变化并更新 Array 值

- (BOOL)textField:(UITextField *)textField 
            shouldChangeCharactersInRange:(NSRange)range 
            replacementString:(NSString *)string 

    [[TableViewController instance].labelArr replaceObjectAtIndex:self.tag withObject:textfield.text];

    return YES;

【讨论】:

在 CNExpandingCell.m 中使用 labelArr 和实例方法时出现以下错误 Property labelArr not found on object id and No known class method for selector 'instance' @property (nonatomic,retain) NSMutableArray * labelArr; + (id) 实例;上面几行需要在TableView.h文件中添加 在 CNExpandingCell.m 中使用 labelArr 时出现以下错误 在 TableViewController.h 中添加对象 id 后找不到属性 labelArr 你需要#import "TableViewController.h" 到CNExpandingCell.m 我已经导入它仍然显示相同的错误属性 labelArr not found on object of type id

以上是关于将文本字段数据保存在 xcode 中的可扩展表格单元格中的主要内容,如果未能解决你的问题,请参考以下文章

Django自动将文本字段中的双引号转换为单引号

点击保存按钮并存储在字典中时获取表格视图单元格中的文本字段?

从表格视图中的重复文本字段中保存用户输入?

Xcode ios 表格视图

重用自定义UITableViewCell,如何获取每个单元格文本字段的文本进行保存?

如何将充满文本字段的 HTML 表的更改保存到数据库