Top of the page
Close

This is a red error

This is a red error

This is a red error

Editing UITextView in UITableViewCell via delegate method

Ondrej Rafaj on 2009.12.04 17:53:49

Today I'll show you how to create a delegate method that can pass data back to the parent class.

It actually does not matter if you are using this in the cell in table view or you are just exchanging data between different type of the class ... It' always the same.

First you have to create the UITableViewCell class that will contain your delegate function.

This delegate function is responsible for passing data back to your class ... for example, - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath function that you definitely have to know :) is a delegate method as well ... its passing the information abou what row you have selected back from the table view intor class.

Code for the EditFieldCell.h:

//
//  EditFieldCell.h
//  iBlog
//
//  Created by Ondrej Rafaj on 12.11.09.
//  Copyright 2009 Home. All rights reserved.
//

#import 

@class EditFieldCell;

@protocol EditFieldCellDelegate 

// we will make one function mandatory to include
- (void)editDidFinish:(NSMutableDictionary *)result;

@optional
// and the other one is optional (this function has not been used in this tutorial)
- (void)editStarted:(UITextField *)field;

@end


@interface EditFieldCell : UITableViewCell  {
	
	IBOutlet UITextField *textField;
	
	id  delegate;
	
	NSString *key;
	
}

@property (nonatomic, retain) IBOutlet UITextField *textField;

@property (nonatomic, assign) id  delegate;

@property (nonatomic, retain) NSString *key;


@end

Code for the EditFieldCell.m:

//
//  EditFieldCell.m
//  iBlog
//
//  Created by Ondrej Rafaj on 12.11.09.
//  Copyright 2009 Home. All rights reserved.
//

#import "EditFieldCell.h"


@implementation EditFieldCell

@synthesize textField, delegate, key;



- (void)textFieldDidBeginEditing:(UITextField *)tf {
	[[self delegate] editStarted:tf];
}

- (void)dealloc {
	[textField, key release];
    [super dealloc];
}


@end

You have to of course create a new cell view in the Interface Builder and assign all the necessary variables onto your class :) ... I assume that you already know that stuff when you are reading a tutorial about delegate methods :))

and this is how to set the delegate in the main view:

@interface EditBlogController : BasicTableController 

And we can't forget the function that will receive your data:

- (void)editDidFinish:(NSMutableDictionary *)result {
	NSString *key = [result objectForKey:@"key"];
	NSString *value = [result objectForKey:@"text"];
}

And finally here we have the cellForRowAtIndexPath function:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"MyCell";
	EditFieldCell *cell = (EditFieldCell *) [super.table dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil){
		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EditFieldCell" owner:nil options:nil];
		for(id currentObject in topLevelObjects) {
			if([currentObject isKindOfClass:[EditFieldCell class]]) {
				cell = (EditFieldCell *) currentObject;
				break;
			}
		}
	}
	int index = [indexPath indexAtPosition: [indexPath length] - 1];
	// next line is very important ... 
	// you have to set the delegate from the cell back to this class
	[cell setDelegate:self];
	// I am using the key to identify what field is user editing
	NSString *key = @"key_for_your_value";
	NSString *placeholder = @"Your placeholder";
	cell.key = key;
	cell.textField.text = [blogData objectForKey:key];
	cell.textField.placeholder = placeholder;
    return cell;
}

As I said before, I'm assuming that you already know how to create a table view and display data in it, but if you'll have any question please feel free to post a comment, I'll be happy to help you with that :).

Cheers,

Ondrej

Ondrej Rafaj

Ondrej Rafaj

Technical director @ Fuerte International

Back to top Comment

Comments ... Why not to get involved!
HTML Comment Box is loading comments...

Old comments:

  • Felix

    Felix

    from 203.45.**.** @   

    Hi, I stumbled upon your tutorial and you have a great tutorial about the UISwitch. It helped me a lot.

    I have a question though. I want one view to contain a TextView. When I drag a textview I got a lorem paragraph, right? I edited it and put my own content but I want to have more than 1 paragraph. How can you do more than 2 paragraph? I tried using Command+Enter, it works but when you get your mouse out of the textview it will go back to 1 paragraph. When you Build and Run, you will see your content in just 1 paragraph.

    Is there any way you can display contents that contain more than 1 paragraph?

    I am actually trying to put an FAQ page on my apps.

    I hope you can help.

  • Ondrej

    Ondrej

    from 90.192.**.** @   

    I had the same problem. Just edit the text anywhere else and use Ctrl+C / V to put it back to the text view :)

  • Felix

    Felix

    from 203.45.**.** @   

    Thank, it works!

    Does anybody have a sample program using UIWebView where you have a progress bar while waiting for the page to completely load?

  • Ondrej

    Ondrej

    from 87.194.**.** @   

    Check the documentation for UIWebviewDelegate ... think that there is just did start and did Finished loading function available ...

    http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html

  • Ondrej

    Ondrej

    from 87.194.**.** @   

    That means that u can still use the UIActivityIndicator

  • marioaae

    marioaae

    from 190.148.**.** @   

    Hello, could you please upload the whole project? It's being hard to me to make it work in my project, I think if I see how you use it, it's going to help me a lot! Thanks!!

  • Ondrej

    Ondrej

    from 87.194.**.** @   

    I don't have a tutorial project right now, but I'll try to put something together over the weekend ... stay tuned :)

Back to top

IE SUCKS, DON'T USE IT :) Digg this page