2011-12-08 27 views
0

我的UIButton在我的UITableViewCell。此按鈕啓動UIActionSheet,因此當用戶從操作表中按下按鈕時,我希望按鈕的文本更改爲該文本。從UIActionSheet更新UIButton文本

我用下面的代碼顯示按鈕。我想如果我在actionSheet: clickedButtonAtIndex:中調用'tableView reload'會有效,但是有更好的選擇來更新statusButton嗎?

SmokingStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.statusButton.titleLabel.text = [self.vitalsDictionary objectForKey:@"smoking_status_display"]; 


- (IBAction)smokingStatusButtonClicked:(id)sender{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Smoking Status" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"1 Current everday smoker", @"2 Current some day smoker", nil]; 
    self.smokingStatusActionSheet = actionSheet; 
    [self.smokingStatusActionSheet showInView:self.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
    if (actionSheet == self.smokingStatusActionSheet){ 
     switch (buttonIndex) { 
      case 0: 
       [self.vitalsDictionary setObject:@"daily_smoker" forKey:@"smoking_status"]; 
       break; 
      case 1: 
       [self.vitalsDictionary setObject:@"nondaily_smoker" forKey:@"smoking_status"]; 
       break; 
      default: 
       break; 
     } 
    } 
} 

回答

2

您應該更新您的數據源,然後您可以在需要時再次重新配置該單元。

如果單元格不在屏幕上,它會在您的tableView:cellForRowAtIndexPath:實現中自動更新,當它再次變爲可見時。

如果你想迫使它更新,它是可見的,您可以:

  1. 呼叫reloadData

  2. 呼叫reloadRowsAtIndexPaths:withRowAnimation:

  3. 呼叫cellForRowAtIndexPath:和配置。

對於2 + 3,您需要保持對調用操作表的單元格的索引路徑的引用。

如果選擇3,它可以幫助有你tableView:cellForRowAtIndexPath:方法設置像這樣:

- (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]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; 
{ 
    //..configure label etc here 
} 

通過分隔configureCell:atIndexPath:,你可以使用相同的代碼可以將單元配置。

例如,您可以撥打

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{  
    if (actionSheet == self.smokingStatusActionSheet){ 
     switch (buttonIndex) { 
      case 0: 
       [self.vitalsDictionary setObject:@"daily_smoker" forKey:@"smoking_status"]; 
       break; 
      case 1: 
       [self.vitalsDictionary setObject:@"nondaily_smoker" forKey:@"smoking_status"]; 
       break; 
      default: 
       break; 
     } 
    } 

    NSIndexPath *indexPath = self.selectIndex; 
    UITableViewCell *cell = [self.tableView cellForRowAtPath:indexPath]; 
    [self configureCell:cell atIndexPath:indexPath]; 
} 
+0

我擁有的代碼非常複雜,我寧願不將這些方法分開。有沒有辦法更新包含按鈕的特定單元格?它的'indexPath'是9. – Jon

+0

就我所能想象的那樣,在分離代碼時不應該有任何困難,因爲在你取回一個單元格之後,在你返回並粘貼到新方法中之前,它將被刪除。但無論如何,indexPath 9沒有這樣的東西。indexPath可以有很多索引,但是對於tableview它們通常只有兩個'section'和'row'。我會從你的「9」中假設你只有一個部分,所以爲了得到那個單元格,你需要'[NSIndexPath indexPathForRow:9 inSection:0]對這些硬編碼的魔術數字保持警惕,他們很可能會咬人你以後。 –

+0

謝謝。我嘗試過,但它似乎沒有做任何事情:'NSArray * array = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:9 inSection:0]]; [self。vitalsTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];' – Jon

0

您可以撥打-reloadRowsAtIndexPaths:withRowAnimation:在桌子上,或乾脆直接獲取到單元格的引用( - [UITableView中的cellForRowAtIndexPath:]),並設置按鈕上的文字。

+0

如何爲陣列設置有關係嗎?包含該按鈕的單元格的indexPath是9.'NSArray * array = [NSArray arrayWithObject:9]; [self.vitalsTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];' – Jon