2011-09-30 64 views
2

我面臨的問題是,有什麼方法可以更改UITableView中刪除按鈕的默認框架 ,還有一點是,刪除按鈕始終顯示在單元格的中心UITableViewcell所以任何方式來改變刪除按鈕的框架。iPhone:如何更改UITableViewCell中的默認刪除按鈕的框架

// Update the data model according to edit actions delete or insert. 
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [appDelegate.categories removeObjectAtIndex:indexPath.row]; 
     [categoryTableView reloadData]; 
    } 
} 

#pragma mark Row reordering 
// Determine whether a given row is eligible for reordering or not. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

// Process the row move. This means updating the data model to correct the item indices. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
     toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    NSString *item = [[appDelegate.categories objectAtIndex:fromIndexPath.row] retain]; 
    [appDelegate.categories removeObject:item]; 
    [appDelegate.categories insertObject:item atIndex:toIndexPath.row]; 
    [item release]; 
} 

在此先感謝。

回答

0

您可以通過設置單元的editingAccessoryView屬性來使用自己的視圖。這可以有你喜歡的任何框架,但是如果你想讓它看起來像標準的刪除按鈕,你需要提供一個漸變。

在自定義單元格的xib文件中,添加一個任意大小的新按鈕(只要它小於單元格!)。此按鈕不應該是您的單元格的子視圖,而應該是文件中的單獨項目。

將此按鈕連接到您的手機的editingAccessoryView插座,它將替換標準刪除按鈕。

+0

請告訴我教程或任何鏈接 –

+0

您的單元格是從xib文件創建還是默認類型之一? – jrturton

+0

我正在創建單元格(CustomCell),它是UITableViewCell的一個子類 –

3

在ios 6.1中,我無法使用上面的代碼。我見過幾個答案,建議繼承這個單元類並提供一個自定義 - willTransitionToState。改變子視圖/按鈕視圖框架不會有意改變它的顯示位置。

但是,如果您覆蓋layoutSubviews方法它將工作。這裏是代碼:

//below i moved the delete button to the left by 10 points. This is because my custom tableview has lef 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    for(UIView *subview in self.subviews) 
    { 
     if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) 
     { 
      CGPoint o = subview.center; 
      subview.center = CGPointMake(o.x - 10, o.y); 
     } 
    } 
} 
相關問題