user198725878
user198725878

Reputation: 6386

Error with NSString

I have created a uitext view as below:

in the h.file
@property (nonatomic, retain) NSString *strDescription;

in the m file

@synthesize strDescription;

- (void)viewDidLoad
{
   GRect frame = CGRectMake(0, 0, 320, 369);
    tblAddEquipment = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    tblAddEquipment.delegate = self;
    tblAddEquipment.dataSource = self;
    //avoid reusable
    [self.view addSubview:tblAddEquipment];
    self.tableView.scrollEnabled = YES;
    //self.tblAddEquipment.scrollEnabled = NO;
    [tblAddEquipment release];
    [self.tableView setSeparatorColor:[UIColor clearColor]];

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    strDescription = textView.text;
    NSLog(@"strDescription textView#####################--> %@", strDescription);
    [tblAddEquipment reloadData];
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSLog(@"str Description in tableview--> %@", strDescription);

      UITextView *txtDescription;

                   cellRectangle = CGRectMake( 175, 1, 120, 40 );               
                    txtDescription = [[UITextView alloc] initWithFrame: cellRectangle];
                    txtDescription.font = font;
                    //txtDescription.scrollEnabled = YES;
                    txtDescription.textColor = [UIColor blackColor];
                    txtDescription.autoresizingMask = UIViewAutoresizingFlexibleHeight;
                    [cell.contentView addSubview:txtDescription];   
                    txtDescription.returnKeyType = UIReturnKeyDone;
                    txtDescription.delegate = self;
                    txtDescription.tag = 10;
                    NSString *strDesc = strDescription;
                    NSLog(@"strDesc in tableview--> %@", strDesc);
                    txtDescription.text = strDesc;
                    [txtDescription release];


}

When I complete entering the text in the textview, textViewDidEndEditing method gets called.

In that, I get textView text input. I store it in the strDescription variable. I'm able to print it over there. It shows the text assigned to it correctly.

But when the cellForRowAtIndexPath method is called, I tried to print the strDescription variable, but it crashes and shows exc_bad_access.

I have been facing this issue for a long time. I don't know what I'm doing wrong here. Please help me up.

Upvotes: 1

Views: 110

Answers (3)

MusiGenesis
MusiGenesis

Reputation: 75276

Try changing the first line in textViewDidFinishEditing to this:

strDescription = [NSString stringWithFormatString:@"%@", textView.text]; 

Upvotes: 0

TheRonin
TheRonin

Reputation: 1315

First, this method

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

need to return UITableViewCell object i think…

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

You have retained strDescription but in your code it seems me, you are not using that, because the below statement will not increment the retain count,

strDescription = textView.text;

Use below instead

self.strDescription = textView.text;

Use below code

- (void)textViewDidEndEditing:(UITextView *)textView
{
    self.strDescription = textView.text;
    NSLog(@"strDescription textView#####################--> %@", strDescription);
    [tblAddEquipment reloadData];
 }

Upvotes: 2

Related Questions