2010-11-16 69 views
0

我對iPod的編程還很陌生,所以請耐心等待。我是否需要爲此構建自定義的UITableViewCell?

我試圖在cellView中有按鈕,每個單元格有2個值中的一個,a +和a - 。意思是,按鈕會顯示+,當你點擊它時,它會跳到 - 。

我設法讓按鈕工作並更改底層數據,但我在重繪表時遇到了麻煩。

我添加爲註釋,我需要將該按鈕的文本更改爲源代碼。這可以做到,或者我必須建立一個自定義的uitableviewcell?

Tia,Stefano。

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    //some code 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SectionsTableIdentifier ]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:SectionsTableIdentifier] autorelease]; 
     UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"]; 
     UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height); 
     [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; 
     [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted]; 
     if (some condition) [button setTitle:@"+" forState:UIControlStateNormal]; 
     else [button setTitle:@"-" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = button; 
    } 
    if (someCondition) { 
     cell.textLabel.text = @"text1"; 
    }else { 
     cell.textLabel.text = @"text2"; 
    } 

    //I NEED TO CHANGE THE TEXT FOR THE BUTTON HERE. 

    return cell; 
} 

回答

2

因爲你正在做的按鈕細胞的accessoryView的,你可以這樣做:

UIButton *accButton = (UIButton *)cell.accessoryView; 
[accButton setTitle:@"New Text" forState:UIControlStateNormal]; 
+0

哦,我錯過了這個答案,或者我wouldn't've貼我的。 Upticked。 – 2010-11-16 23:22:56

+0

接受Steven's的回答只是因爲它早些發佈。 完美的作品,非常感謝。學習Objective-C和Iphone API同時有時候是一種壓倒性的。 – blindstuff 2010-11-17 14:58:30

0

您需要按照設置背景圖像的相同方式設置文本 - 您必須將其設置爲特定控件狀態。

如果您只是嘗試更改myButton.text,它將不會顯示出來。因此

正確的代碼是:

[button setText:@"Some Text" forState:UIControlStateNormal]; 
0

我相信你滾動表時上下也有問題。所有的單元都有相同的標識符,它們將在刷新時重用。這意味着單元不會總是零。因此,配件按鈕標題應在條件後更新:if (cell == nil) {}

0

您需要將代碼設置爲按鈕的文本,並將其設置爲您的評論所在的位置。您的問題在於訪問您沒有指向的按鈕。一種方法是在設置單元格時爲按鈕分配標籤,然後請求具有該標籤的子視圖以後再獲取該單元格。

更好的方法是使用自定義表格視圖單元格,如您所示。它沒有太多的工作,如果你提供一個屬性,你可以直接訪問它的內容。而且,你可能會將狀態變化的邏輯推入該類。

1

cell通常不會是零,這意味着你的設置代碼通常會被跳過。您需要確保您的每個單元格設置代碼在條件中是而不是,但在其後面的代碼中。

因此,像這樣:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    //some code 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SectionsTableIdentifier ]; 
    if (cell == nil) { 

     // Note that here, we set up only the things that will not change. 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:SectionsTableIdentifier] autorelease]; 
     UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"]; 
     UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height); 
     [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; 
     [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted]; 
     [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = button; 
    } 

    UIButton *button = (UIButton *)cell.accessoryView; // get button back, regardless of if we went through init above 
    if (someCondition) { 
     cell.textLabel.text = @"text1"; 
     [button setTitle:@"+" forState:UIControlStateNormal]; 
    }else { 
     cell.textLabel.text = @"text2"; 
     [button setTitle:@"-" forState:UIControlStateNormal]; 
    } 

    return cell; 
} 
+0

正確的答案,我沒有標記它,因爲aBitObvious第一次發佈。謝謝您的幫助。 – blindstuff 2010-11-17 16:23:32

+0

很酷。無論如何,如果我第一次看到那個,我不會發布我的。謝謝! – 2010-11-17 23:10:35

相關問題