Velocity Velocity - 1 month ago 11
iOS Question

Passing data from table view

in my iOS app the user can select a hostname in a table view. This hostname should be transferred to a detail view controller.

TableViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UITableView *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@", self.cell.textLabel.text);
DPPingViewController *objectOfPingViewController = segue.destinationViewController;
objectOfPingViewController.string = (NSString *)sender;
}


DetailViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.ipAddr.text = _string;
//self.ipAddr.text = @"192.168.178.1";
self.results.text = @"";
NSLog(@"Zelle: %@", _string);

}


NSLOG

2014-07-15 11:52:37.646 EasyTest[3623:90946] Zelle: <UITableViewCell: 0xc17fe30; frame = (0 0; 320 44); text = 'Apple.com'; autoresize = W; layer = <CALayer: 0xc1800c0>>


Why doesn't the NSLog shows only "Apple.com". Where does the other informations come from?

Answer

It looks like the sender for the segue is actually the UITableViewCell, and not the string content you want, which would make sense from how you configure it in the story board. Instead of casting the sender to an NSString, you probably want to do something like:

UITableViewCell *sourceCell = (UITableViewCell *)sender;
objectOfPingViewController.string = sourceCell.textLabel.text;

That should get you the actual string content, rather than an entire table cell, which is a view class with a bunch of other parameters along for the ride.

Comments