2012-08-02 34 views
1

嘿,我正在處理一個在動態tableView中發佈50個位置的應用程序,當您點擊某個位置時,它將繼續播放到新的tableView控制器,並從該位置發佈50張照片。我創建了一個tableViewController,然後創建了一個新文件,其中包含一個tableView需要IE Cellforrowatindexpath的所有文件。我有從主tableViewcontroller連接的segue,但所有的信息都存儲在newfile中,該文件包含tableViewController使用的方法。我是否在tableViewController中編寫PrepareForSegue,或者將它寫入具有創建表的方法的文件中?如果我將它寫入tableViewCOntroller,我該如何訪問其中一個動態創建的單元格的單元名稱?謝謝。在哪裏打電話準備在表格中查看

-(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!? 
    } 
} 

呼叫名字來源於它使用的公共API來創建具有信息,如名稱和位置的字典的數組另一個文件。該文件被稱爲flickrFetcher。這是動態創建單元的代碼。 self.brain是flickerFetcher的一個實例,topPlaces是從flickrFetcher調用來獲取NSArray的NSArray的方法。

 - (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; 
} 

回答

1

prepareForSegue:UIViewController的方法,所以它需要在視圖控制器。 sender參數應該是導致segue的您的單元格。您可以使用表格視圖方法indexPathForCell:來獲取相關的索引路徑,並且在執行cellForRowAtIndexPath:時應該足以找到放入單元格的相同數據。

(我不知道你所說的「新文件」或什麼類它實現的意思,因此,如果這會影響什麼,我不能說。)

+0

通過新的文件我的意思是我做的一個子類它 – 2012-08-02 22:07:07

+0

好的,如果它是一個子類,意味着它也是一個'UIViewController',所以無論你將方法放在父對象還是子對象中,都不應該產生功能差異。孩子可能會更好地訪問您想要傳遞給新控制器的信息......我猜測。 – 2012-08-02 22:10:28

+0

當我把prepareforsegue在父母,然後寫入NSIndexPath * indexPath = [self.tableView indexPathForCell:sender];我得到一個錯誤,聲明tableview沒有被聲明。但是當它在孩子身上時,這個錯誤被清除了。如果索引路徑只返回行和段,我該如何獲得調用名稱? – 2012-08-02 22:17:09