Question

i'm new to iOS programming , i want to create a expandable table cell with a text field inside it.i have to save the text entered in each of the cell uniquely but when i enter some text in the text field and move to another cell the text is deleted. i have also posted my code here.

help me to resolve this issue thanks in advance

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
Was it helpful?

Solution

My way of doing it would be like this:

Create a singleton instance of the TableViewController where you place a array storing the content of the labels.

@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

Then in the CNExapndingTableViewCell check when the UITextField value changes and update the Array value

- (BOOL)textField:(UITextField *)textField 
            shouldChangeCharactersInRange:(NSRange)range 
            replacementString:(NSString *)string 
{
    [[TableViewController instance].labelArr replaceObjectAtIndex:self.tag withObject:textfield.text];

    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top