2014-09-05 47 views
0

我的tableOne包含名爲CustomCellForTableOne(.h和.m)的自定義UITableViewCell。當用戶點擊tableOne中的一個單元格時,我希望它們繼續到tableTwo。到目前爲止這麼容易。iOS:以編程方式從現有單元格創建表格「標題」

我希望tableOne中的單元格成爲tableTwo中的表格標題。有沒有一個簡單的方法來做到這一點?理想情況下,我想從通過segue傳遞的數據中簡單創建一個CustomCellForTableOne,並將該單元格分配爲我的tableTwo的頭部。

  1. 我不知道該怎麼報頭分配到表編程(這樣我就可以嘗試)
  2. 它的工作? (因爲我還沒有嘗試過,所以我想我可能會要求節省一些時間)或者我必須採取一些不同的方法嗎?
+0

謝謝大家的好回答:+1。我將根據哪一個最適合我來執行並報告回來。非常感謝你的幫助。 – learner 2014-09-05 21:15:19

回答

1

它應該是可能的單元格和標題視圖的表都從UIView繼承。在你的tableTwo中實現以下內容並從中返回你的表視圖單元實例。 -

- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section 
+0

我需要類似的東西,但這種方法從來沒有被調用過。 – 2014-09-06 17:56:42

+0

實現UITableViewDelegate和UITableViewDataSource – Avi 2014-09-06 23:39:27

1

表頭視圖很難處理。我會在第二個表格中放置一個額外的部分,僅用於已經通過的特定單元格。這可能是第二張桌子的第一部分,您可以將它設計爲看起來像一個標題。

另一種方法是創建一個普通的視圖,並將其放置在第二個表的標題中(這是完全合法的)。然後,您可以通過addSubView:在該普通視圖中安全地添加特定單元格(如果它有視圖)。

1

我是能夠實現你正在尋找在第一視圖控制器下面的代碼做什麼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // create the new table view 
    TestTableViewController2 *testView2 = [TestTableViewController2 new]; 

    // get a reference to the cell that they clicked and create a totally new cell, a 'copy' of 
    // the original that we can pass to the next view 
    UITableViewCell *clickedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    UITableViewCell *clickedCellCopy = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 
    clickedCellCopy.textLabel.text = clickedCell.textLabel.text; 
    clickedCellCopy.detailTextLabel.text = clickedCell.detailTextLabel.text; 

    // set this new cell as the header cell on the new view 
    testView2.myHeaderCell = clickedCellCopy; 

    // push the new view onto the stack 
    [self.navigationController pushViewController:testView2 animated:YES]; 
} 

然後將屬性添加到您的第二個表格視圖控制器:

@property (nonatomic, retain) UITableViewCell *myHeaderCell; 

然後在.M的viewDidLoad:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // set our header cell as the table view's header 
    self.tableView.tableHeaderView = self.myHeaderCell; 
} 

這給了我下面的結果:

https://www.dropbox.com/s/t42lraue1kfolf3/cell%20demo.mov?dl=0

0

你可以在廈門國際銀行文件創建自定義的UITableViewCell並加載它爲每個視圖。你將不得不在兩個視圖控制器中創建它。取決於複雜程度,你也可以完全用代碼完成。

相關問題