开发者

how can I copy a string displayed in a UITableViewCell into another NSString?

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?


Try something like this:

NSString *anotherString = [NSString stringWithString:cell.textLabel.text];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜