2009-07-02 29 views
8

我想向表格單元格添加一個按鈕。 「刪除事件」中的日曆應用程序激發了我...(一個類似的例子是「共享聯繫人」中的聯繫人)UITableView單元格中的UIButton類似於「刪除事件」

截至目前有

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //..yadayadayada 
    cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"buttonCell"] autorelease]; 
    } 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark]; 
    [button setBackgroundColor:[UIColor redColor]]; 
    button.titleLabel.text = @"Foo Bar"; 
    [cell.contentView addSubview:button]; 

其產生的按鈕,確實如此。它看起來並不怎麼樣(很明顯,我從來沒有在iPhone中處理過按鈕),但是這至少是正確的方法嗎?

+0

按鈕的合成動作取決於它所在的行是否會有所不同? (例如,通過電子郵件發送當前聯繫人在這一行,等等......) – 2009-07-02 21:51:00

+0

如果是這樣,你可以設置每個單元格按鈕的標籤,indexPath行,然後在選擇器中讀取發件人的標籤,觸摸。 – mmc 2009-07-02 21:54:46

+0

那麼,就像「刪除事件」一樣,我只在UI的底部有一個按鈕,即最後一行。 – 2009-07-02 21:59:24

回答

1

是的,這通常是正確的方法。

小費:

  • 設置回調您的按鈕事件,因此,它被點擊時實際上做了什麼。

    [myButton addTarget:self action:@selector(whatMyButtonShouldDo:) 
        forControlEvents:UIControlEventTouchUpInside]; 
    

編輯:使用系統按鈕,這使得很多的東西我寫uneccessary調整代碼。

+0

在他的示例代碼中,不需要釋放按鈕,因爲[UIButton buttonWithType:]返回一個自動釋放實例。 – 2009-07-02 21:45:57

1

是的,你在正確的軌道上,這是添加子視圖到單元格的最簡單的方法(另一個是子類化UITableViewCell)。

查看Apple guide瞭解更多信息。

19

每次顯示一個單元格時,您現在都擁有它的方式是分配一個按鈕,設置它的值並將其添加到單元格的contentView中。當單元格被重用時(通過dequeueReusableCellWithIdentifier),您將創建另一個新按鈕,將它添加到單元格(在舊單元格頂部)等等。事實上它已經通過addSubview但沒有顯式釋放意味着每個按鈕都保留計數永遠不會歸零,所以他們都會堅持下去。經過一段時間的滾動上下單元格將最終成百上千的按鈕子視圖,這可能不是你想要的。

一些提示:

除非當 dequeueReusableCellWithIdentifier將返回零和你初始化單元的完成
  • 不要在一個cellForRowAtIndexPath調用內部分配的東西。隨後的所有其他時間,您都會將已經設置的緩存單元交還給您,因此您只需更改標籤或圖標即可。您將要在單元格分配代碼之後的條件權限內將所有按鈕分配的內容移動到if之內。

  • 該按鈕需要有一個位置,也爲它設置的目標,所以它會做點什麼,當點擊。如果每個單元格都有這個按鈕,那麼一個巧妙的技巧就是讓它們都指向相同的目標方法,但將該按鈕的值設置爲單元格的indexPath.row(單元分配塊之外,因爲它對於每個單元都不相同)。該按鈕的常見tap處理程序將使用發件人的標記值查找dataSource列表中的基礎數據。

  • 在您完成addSubview後,在按鈕上撥打電話release。這樣,保留計數就會降到零,並且當父對象被釋放時對象實際上會被釋放。

  • 而是通過addSubview添加按鈕,你可以返回它作爲accessoryView的細胞,所以你不必擔心它定位(除非你已經在使用accessoryView的其他東西 - 像披露鈕釦)。

7

我採取了另一種方法在日曆應用程序中創建等效於「刪除事件」按鈕的方法。我沒有添加按鈕作爲子視圖,而是向單元格添加了兩個背景視圖(紅色和深紅色,漸變效果很好),然後將角落四捨五入並將邊框設置爲灰色。

下面的代碼創建一個可重用的單元格(以通常的方式)。引用的兩個圖像('redUp.png'和'redDown.png')取自日曆的「刪除事件」按鈕的屏幕截圖。 (這似乎比以編程方式創建漸變更快)。可以稍微調整一下,以使其更接近日曆的「刪除事件」外觀,但這非常接近。

該按鈕的動作由tableView委託方法tableView:didSelectRowAtIndexPath:方法觸發。

// create a button from a table row like the Calendar's 'Delete Event' button 
// remember to have an #import <QuartzCore/QuartzCore.h> some above this code 

static NSString *CellWithButtonIdentifier = @"CellWithButton"; 

UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:CellWithButtonIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithButtonIdentifier] autorelease]; 
    [[cell textLabel] setTextAlignment: UITextAlignmentCenter]; 

    UIImageView* upImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redUp.png"]]; 
    UIImageView* downImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redDown.png"]]; 

    [cell setBackgroundView: upImage]; 
    [cell setSelectedBackgroundView: downImage]; 

    [[upImage layer] setCornerRadius:8.0f]; 
    [[upImage layer] setMasksToBounds:YES]; 
    [[upImage layer] setBorderWidth:1.0f]; 
    [[upImage layer] setBorderColor: [[UIColor grayColor] CGColor]]; 

    [[downImage layer] setCornerRadius:8.0f]; 
    [[downImage layer] setMasksToBounds:YES]; 
    [[downImage layer] setBorderWidth:1.0f]; 
    [[downImage layer] setBorderColor: [[UIColor grayColor] CGColor]]; 

    [[cell textLabel] setTextColor: [UIColor whiteColor]]; 
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; 
    [cell setBackgroundColor:[UIColor clearColor]]; // needed for 3.2 (not needed for later iOS versions) 
    [[cell textLabel] setFont:[UIFont boldSystemFontOfSize:20.0]]; 

    [upImage release]; 
    [downImage release]; 
} 
return cell; 
相關問題