2013-10-28 45 views
2

我很新到iOS,但我已經創建了我要投入到編輯模式表視圖視圖控制器。這是我的視圖控制器實現文件代碼:iOS應用 - 刪除按鈕不顯示時表視圖處於編輯模式

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize progsTable, programmes; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    programmes = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil]; 
    //set the title 
    self.title = @"Programmes"; 

    //add an edit button 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [progsTable setEditing:editing animated:animated]; 
} 

#pragma mark - UITableView Datasource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return programmes.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(cell==nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.text = [programmes objectAtIndex: indexPath.row]; 

    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) indexPath { 
    return YES; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle==UITableViewCellEditingStyleDelete) { 
     //remove from array 
     [programmes removeObjectAtIndex:indexPath.row]; 

     //remove from table 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
#pragma mark - UITableView Delegate methods 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

@end 

我得到了頂部的編輯按鈕離開,但是當我點擊紅色的刪除按鈕不會出現 - 我究竟做錯了什麼?

+0

順帶它是基於本教程http://www.youtube.com/watch?v=NHzm-D9VsQA - 我想我已經按照他的指示準確,但它不是爲我工作。我使用的是故事板的Xcode 5,而本教程使用廈門國際銀行文件 - 會是問題? –

回答

2

運行你確切的代碼做的工作 - 當我測試它的刪除按鈕顯示。

我認爲,問題是,你還沒有分配的@property叫progsTable你的表視圖。

爲了解決它做到以下幾點:

  • 轉到你的瀏覽器你的故事板
  • 右鍵
  • 單擊並按住+旁邊叫progsTable出口同時拖動鼠標你的表視圖,就像這樣:

enter image description here

嘗試運行您的應用程序 - 現在應該顯示刪除按鈕。

+0

謝謝,這工作! –

+0

不客氣。很高興我能幫上忙。 –

2

我認爲你需要添加下面的代碼:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
return UITableViewCellEditingStyleDelete; } 

這告訴你想要的編輯風格的UITableView。

1

使用此

- (IBAction)deleteDrug:(id)sender event:(id)event { 
    selectedButtonIndex = [self indexPathForButton:sender event:event]; 

    [tableView setEditing:YES animated:YES]; 
} 


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath == selectedButtonIndex) { 
     return YES; 
    } 

    return NO; 
} 

您可能需要實現您的數據源此方法。

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
0

檢查您是否還在界面構建器中添加了自動佈局約束,以便單元格的最後端與您的設備寬度對齊。

運行在模擬器iPad上的代碼,如果刪除按鈕有但不顯示出來的iPhone模擬器,那麼它可能只是內容沒有正確對齊到設備的視口。

我的'刪除'按鈕沒有顯示出來,但是當我滑動時單元格標題內容向左移動,但是我沒有在我的故事板中對單元格寬度設置約束,只要我設置了右鍵菜單,邊界約束爲'0'或'16'時,出現刪除按鈕。

相關問題