Terrel Gibson
Terrel Gibson

Reputation: 481

Where to call prepare for segue in a table View

Hey I am working on an application that posts 50 location in a dynamic tableView and when you click on a location it will segue to a new tableView controller and posts 50 photos from that location. I created a tableViewController and then created a new file which contains all the files a tableView requires IE Cellforrowatindexpath. I have the segue connecting from the main tableViewcontroller but all the information is stored in the newfile which contains the methods that the tableViewController uses. Do I write PrepareForSegue in the tableViewController or do I write it in the file which has the methods that create the table? also if I write it in the tableViewCOntroller how do I access the cell name for one of the cells that are dynamically created? thanks.

 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"Photos for location"]){
//I dont know what to use for the name
        [segue.destinationViewController setPhotoInPlace: WHAT DO I CALL THIS!?
    }
}

The call names come from another file which uses public API to create an array of dictionaries which have information such as name and location. The file is called flickrFetcher. Here is code that dynamically creates the cells. self.brain is an instance of flickerFetcher and topPlaces is the method called from flickrFetcher to get the NSArray of NSdictionaries.

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
// Create an instance of the cell
UITableViewCell *cell;
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Photo Description"];

if(!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Photo Description"];
// set properties on the cell to prepare if for displaying   
//top places returns an array of NSDictionairy objects, must get object at index and then object for key

// the cellTitle has country then province, country goes in main title and province will go as subtitle.
NSString * cellTitle = [[[[self.brain class] topPlaces] objectAtIndex:self.location] objectForKey:@"_content"]; 

NSRange cellRange = [cellTitle rangeOfString:@","];

NSString * cellMainTitle = [cellTitle substringToIndex:cellRange.location];

cell.textLabel.text = cellMainTitle;

NSString* cellSubtitle = [cellTitle substringFromIndex:cellRange.location +2];

cell.detailTextLabel.text =  cellSubtitle;
//location is an int property that allows a new selection when using objectAtIndex
self.location++;
return cell;
}

Upvotes: 1

Views: 3200

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

prepareForSegue: is a UIViewController method, so it needs to be in your view controller. The sender parameter should be your cell that caused the segue. You can use a table view method indexPathForCell: to get the related index path and that should be enough to find the same data you put into the cell when you implemented cellForRowAtIndexPath:.

(I'm not sure what you mean by "a new file" or what class it implements, so I can't say if that affects anything.)

Upvotes: 1

Related Questions