2011-12-08 25 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