2012-03-02 33 views
1

我搜索,但沒有找到很好的答案是:iOS:UITableView&Back Button,如何更新父視圖中的單元格?

它應該是非常簡單的,我從父TableView中,以一個孩子的TableView實現只在父視圖單元格中選擇選項導航。

在childTableView中,我想將用戶在子視圖中選擇的值(在此視圖中選擇一個單元格)傳遞迴父視圖,並在父視圖中更新「cell.detailTextLabel.text」,我可以發現子視圖中的後退按鈕被點擊後,但無法找到如何/在哪裏更新父視圖中的單元格,並從子視圖的單元格獲取新值。

我的第二個問題是如何將數據傳遞給父視圖。

回答

1

嘗試使用以下

在子視圖,定義一個協議時用戶選擇的小區

// childViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    ... 
    // send info the mainViewController 
    [self.delegate selectedData: (NSString*) [dataSource objectAtIndex: indexPath.row]]; 
} 

在mainViewController

// childViewController.h 
@protocol ChildDelegate; 

@interface childViewController 
{ 
    id <ChildDelegate> delegate; 
} 

@property (nonatomic, weak) id <ChildDelegate> delegate; 
@end // interface 

@protocol ChildDelegate <NSObject> 
    - (void) selectedData: (NSString*) text; 
@end 

發送數據,實現此協議

- (void) selectedData: (NSString*) text 
{ 
    // retrieve current selected row, and update with text. 
} 

//創建childViewController時不要忘記設置代理人

child = // create child 
child.delegate = self; 
// save currently selected row 
// push childViewController 

希望它有幫助。

+0

你如何引用父級TableView來重新加載它?在父TableView的selectedData中,你得到了新的字符串,但你如何強制tableView更新它? – Cam 2012-03-06 16:08:05

+0

更新數據源後,可以使用UITableView的reloadData方法。 – vipinagg 2012-03-06 16:44:46

+0

我知道reloadData,但你如何使用它([tableView reloadData])沒有訪問selectedData中的'tableView'? – Cam 2012-03-06 18:52:16

相關問題