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

时间:2014-04-23 07:43:23

标签: ios xcode uitableview uitextfield

我是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

cell.h

#import <UIKit/UIKit.h>

@interface CNExapndingTableViewCell : UITableViewCell{

}

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

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

@end

cell.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

1 个答案:

答案 0 :(得分:0)

我这样做的方式是这样的:

创建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;
}