【问题标题】:Update text for UIButton from UIActionSheet从 UIActionSheet 更新 UIButton 的文本
【发布时间】:2011-12-08 00:04:24
【问题描述】:

我的UITableViewCell 中有一个UIButton。此按钮启动 UIActionSheet,因此当用户从操作表中拍摄按钮时,我希望按钮的文本更改为该文本。

我使用以下代码显示按钮。我想如果我在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;
        }
    }
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview uibutton


    【解决方案1】:

    您应该更新您的数据源,然后如果需要,您可能会再次重新配置单元格。

    如果单元格不在屏幕上,那么当它再次可见时,它应该在您的tableView:cellForRowAtIndexPath: 实现中自动更新。

    如果你想强制它更新并且它是可见的,你可以:

    1. 致电reloadData

    2. 致电reloadRowsAtIndexPaths:withRowAnimation:

    3. 调用cellForRowAtIndexPath:进行配置。

    对于 2 + 3,您需要保留对调用操作表的单元格的 indexPath 的引用。

    如果您选择 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];
    }
    

    【讨论】:

    • 我的代码真的很复杂,此时我宁愿不分离方法。有没有办法只更新包含按钮的特定单元格?它的indexPath 是 9。
    • 据我想象,分离该代码应该没有困难,因为它只会在您检索单元格之后以及在您返回并将其粘贴到新方法中之前剪切所有代码。但无论如何,没有 indexPath 9 这样的东西。一个 indexPath 可以有很多索引,但对于 tableview,它们通常只有两个 sectionrow。我将从您的“9”中假设您只有一个部分,因此要获得该单元格,您需要 [NSIndexPath indexPathForRow:9 inSection:0] 警惕这些硬编码的幻数,它们很可能会在以后咬你。
    • 谢谢。我试过这个,但它似乎没有做任何事情:NSArray *array = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:9 inSection:0]]; [self.vitalsTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
    • 在您给您的代码中显示smoking_status_display,但更新smoking_status 是这样吗?它们是两个不同的键,因此它们具有不同的数据
    【解决方案2】:

    您可以在表格上调用 -reloadRowsAtIndexPaths:withRowAnimation:,或者直接获取对单元格的引用 (-[UITableView cellForRowAtIndexPath:]) 并设置按钮文本。

    【讨论】:

    • 我该如何设置数组呢?包含按钮的单元格的 indexPath 为 9。NSArray *array = [NSArray arrayWithObject:9]; [self.vitalsTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    相关资源
    最近更新 更多