2011-06-24 13 views
2

我有一個情況,我有一個ViewController包含一個編輯按鈕,在另一個視圖(這是單獨的)包含我的tableview。發送「編輯」到tableview(單獨的視圖)

我目前正在以編程方式添加編輯按鈕(就像使用單個視圖一樣)。然而,正如你點擊編輯按鈕時所期望的那樣,它會變爲'取消',但表格不會進入編輯模式(所有常規方法都已啓用幷包含我將用於單個視圖的代碼)。

我想知道如何從視圖控制器中的編輯按鈕發送'消息'到表格視圖(如上所述是獨立的)。我是否需要設立代表?或者是否有我可以調用的特殊方法?

我已經使用了這個公平的位,但如果任何人都可以幫助我指出正確的方向,我會非常感激。

回答

1

我要指該公司擁有的編輯按鈕,VC1控制器,並擁有控制器(是委託爲)表格視圖爲VC2。另外,請閱讀Delegation。這不是iOS開發中的可選主題。

在VC2,聲明一個方法命名-setTableIsEditing:(BOOL)isEditing,並執行它簡單地設置isEditing財產上的表視圖,就像這樣:

- (void)setTableIsEditing:(BOOL)isEditing { 
    self.myTableView.isEditing = isEditing; 
} 
在VC1的執行按鈕

然後touchUpInside委託,更新伊娃布爾追蹤編輯模式,並呼籲VC2該方法與正確的參數:

- (IBAction)editButtonPressed { 
    _isEditingTable = !_isEditingTable; 
    [self.myVC2Instance setTableIsEditing:_isEditingTable]; 
} 
+0

謝謝:)我很好的閱讀了代表團,只是不確定在這個特殊情況下要做什麼,感謝幫助。得到這一切工作感謝這一點。 – Candyfloss

0

委託可以工作,或者您可以從表視圖將訂閱的按鈕觸動方法發送NSNotification。然後在那裏你可以根據需要爲每個單元調用[UITableViewCell setEditing:YES animated:YES]。

0

使用可以使用

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
      // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

,然後實現

   // Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

// You can use NSUserDefaults for sending information to the controllers in another view 
    //and you can manipulate them. 

} 
+0

這是沒有一絲幫助。你不是說方法去的地方,或者說編輯按鈕屬於與tableview不同的視圖控制器。 – RyanR