2012-05-01 54 views
1

問題: 內置editButtonItem創建一個新的UITableViewController類時,當我刪除註釋斜槓(//)不工作是Xcode的自動註釋掉。通過不起作用,我的意思是編輯按鈕並沒有出現。刷單元也不起作用。editButtonItem沒有顯示出來

嘗試解決方案: 我試圖按照已發佈在其他stackoverflow線程上的各種解決方法無濟於事。我發現的大多數帖子都談論編輯按鈕的各個方面無法正常工作(例如,沒有顯示出來的減號等),但我發現很少有編輯按鈕根本不顯示的帖子。

Hunch: 我有一種預感,它可能與UITableViewController沒有正確實現有關。我對面向對象編程和Objective-C都很陌生,所以我很抱歉如果答案是非常基本的 - 但是,嘿,它是學習過程的一部分。任何幫助深表感謝。

代碼:

_ __ _ .H

#import <UIKit/UIKit.h> 
#import "IndividualRecipeViewController.h" 

@class BrowsePrivateRecipeViewController; 

@protocol BrowsePrivateRecipeViewControllerDelegate 
- (void)browsePrivateRecipeViewControllerDidFinish:(BrowsePrivateRecipeViewController *)controller; 
@end 

@interface BrowsePrivateRecipeViewController : UITableViewController 

@property (weak, nonatomic) id <BrowsePrivateRecipeViewControllerDelegate> delegate; 
@property (assign, nonatomic) NSUInteger listLength; 
@property (strong, nonatomic) NSDictionary *dictionaryOfRecipes; 
@property (strong, nonatomic) NSMutableArray *arrayOfRecipeNames; 

// ... methods 

@end 

_ __ _ .M

@interface BrowsePrivateRecipeViewController() 

@end 

@implementation BrowsePrivateRecipeViewController 

@synthesize delegate = _delegate; 
@synthesize listLength = _listLength; 
@synthesize dictionaryOfRecipes = _dictionaryOfRecipes; 
@synthesize arrayOfRecipeNames = _arrayOfRecipeNames; 

- (void)viewDidLoad 
{ 
    // ... code here 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

// ... other methods 

UPDATE:

LINK TO SOURCE CODE

所以我決定將源代碼發佈到我的整個項目。我遇到了多個文件的問題,但是如果我將它修復爲一個,我很確定剩下的部分將會落實到位。

請關注文件BrowsePrivateRecipeViewController.m/.h.這是最直接的問題。

再次感謝您的耐心和幫助。

真誠, 傑森

+0

看看我的答案。 CodaFi是對的,但是如果他向你展示了一些代碼來修復它,那本來是很好的。我已經包含了一些簡單的代碼來幫助你開始。 –

+1

我會假設他知道如何製作一個UIBarButtonItem,但我確實喜歡每隔一段時間在OP的正確方向上進行一點點推動。 +1 – CodaFi

回答

1

你是不是alloc「荷蘭國際集團或init」荷蘭國際集團的editButtonItem對象,所以你怎麼能指望把它保留下來(等號),更何況已經出現呢?你基本上發消息給零。

2

您需要先做一個按鈕。這將創建一個編輯按鈕,然後將其添加到右邊的BarButtonItem位置。

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)]; 
self.navigationItem.rightBarButtonItem = editButton; 

然後您需要設置一個打開表格編輯模式的方法。

- (void)editTable 
{ 
    [self.tableView setEditing:YES animated:YES]; 
} 

更新:

剛剛看了一遍你的問題,並注意到你想刷卡刪除爲好。您需要添加這些方法才能將其添加到您的tableview。

// Override to support conditional editing of the table view. 
- (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 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

更新2

__的.h

@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar; 

__內容M

@synthesize navigationBar; 

- (void)viewDidLoad { 
    //... 

    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)]; 
    self.navigationBar.navigationItem.rightBarButtonItem = editButton; 
} 
+0

感謝您的快速回復,但按照上述前兩條代碼建議後,該按鈕仍未顯示。我將更新我的原始帖子,向您展示當前代碼的外觀。 –

+0

投入應用程序的屏幕截圖,以便我們瞭解我們正在處理的內容。 –

+0

不幸的是,我有不到10個聲望點,所以我無法發佈截圖。 –

3

首先,我肯定會不使用自定義按鈕編輯表。這是不必要的,只是因爲有一個已經建成。

只需使用UIViewController小號editButtonItem
如果你必須在按下按鈕執行額外的東西,覆蓋-setEditing:animated:,並呼籲super第一。

您在上面提到的錯誤是由於您嘗試訪問不存在的navigationBar的s navigationItem而導致的。您應該訪問您的視圖控制器的navigationItem

self.navigationItem.rightBarButtonItem = self.editButtonItem; 
+0

這就是我試圖做最初(見原職按我的編輯)。由於某種原因,它仍然無法正常工作。我想我只是從頭開始重建整個'UITableViewController',看看我是否不小心編輯了一些我應該保持不動的東西。 –