2017-01-08 38 views
0

我在我的應用程序的菜單中有一個UITableView。該表在開始時具有特定數量的單元格(行)。添加不同類型的單元格到UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 5; 
} 

當例如具有indexPath.row = 1被點擊的單元,我必須添加一些新的細胞(可以說3個細胞)該細胞後(因此在索引2,3和4)。當這個單元格第二次點擊時,我必須刪除添加的單元格。問題是要添加的單元格具有不同的類型(另一個類)。我搜索瞭如何添加/刪除單元格,並且發現我可以使用insertRowsAtIndexPaths和刪除單元格。但是,如何指定單元格的類型然後返回? 這是我的手機的按鈕目標:

[cell.showDetail addTarget:self action:@selector(btnSowDetailClicked) forControlEvents:UIControlEventTouchUpInside]; 

我不知道要放什麼東西在選擇做什麼,我形容:

-(void) btnSowDetailClicked { 
    // ???? 
} 

而且我怎麼能在設定的numberOfRowsInSection選擇?

回答

-1

當您需要添加單元格,初始化並將其存儲到數組時,您可以初始化一個可變陣列來存儲您的單元格,無論您的單元格的類型多少。

numberOfRows方法

,只是在cellforrowatindexpath方法return array.count;

,從你的陣列獲得的細胞,然後返回小區。

0

有你需要跟蹤的,如果你是不是已經幾件事情 -

  1. 按鈕的狀態
  2. 該類電池的數據源返回
  3. 細胞的數量

一個建議是管理一個的NSMutableArray的NSString的(其重新呈現單元類返回)

在按鈕操作中,執行以下操作,其中NSStrings表示特定索引(例如, @ 「的UITableViewCell」 或@ 「MyCustomCell」 等) -

-(void) btnSowDetailClicked { 
    // Toggle button state - prob better to persist in property BOOL cellButtonState 
    cellButtonState = !cellButtonState; 
    // update data source(s) accordingly 
    if (cellButton.selected) { 
      // add NSStrings to data source array 
      // create array of NSIndexPaths to pass to table view 
      // update UITableView with - (void)insertRowsAtIndexPaths:withRowAnimation: 
    } 
    else { 
      // remove NSStrings from data source array 
      // create array of NSIndexPaths to pass to table view 
      // update UITableView with - deleteRowsAtIndexPaths:withRowAnimation: 
    } 
} 

作爲有關上述的UITableView方法文檔中所述 -

的UITableView立即調用相關委託和數據源的方法之後獲取可見細胞的細胞和其他內容。

,因此DataSource可以爲行數返回[dataSourceArray計數]。要獲得正確的類,請返回單元格的位置,在cellForRowAtIndexPath從indexPath中的數據源數組中獲取NSString對象。行並使用NSClassFromString來獲取單元格的類!

相關問題