views:

22

answers:

2

Hey

I am writing a program, and one part of my program has a UItableView in which I set cell contents manually by using the following...

cell.textLabel.text = @"CELL CONTENTS HERE!";

How can I copy a string displayed in a particular TableView Cell into another NSString?

A: 

Try something like this:

NSString *anotherString = [NSString stringWithString:cell.textLabel.text];
Claus Broch
thanks for the reply, but that causes my program to crashHmm, it must be something I'm doing wrong
dBloc
A: 

Keep your @"CELL CONTENTS HERE!" string as a @property of your view controller. Then set the cell's text property to it:

cell.textLabel.text = cellContentsHereProperty;

inside the table view delegate method -tableView:cellForRowAtIndexPath:. You want to do this because you can only access cell.textLabel.text inside this method, or by calling the delegate method to retrieve the cell, which is awkward.

As a general concept, you want your view controller ("control") to keep the string value ("model") separate from how it is displayed ("view"). Keeping things compartmentalized lets you retrieve and change the data without worrying about how it is displayed.

This separation of responsibilities is called the MVC or Model-View-Control design pattern, which Apple subscribes to for iPhone application design.

Alex Reynolds