更改UITableViewCell附件后,UITableViewCellAccessoryDe​​tailDisclosureButton消失

时间:2013-04-02 00:38:04

标签: iphone ios objective-c xcode cocoa-touch

我有一个UITableView,其中包含带有图片,文字和公开按钮的单元格。我正在加载像这样的单元格:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    if ([indexPath row] == [[self multas] count]) {
        [[cell textLabel] setText:@"Carregar mais..."];
    } 
    else 
    {
        Multa *multa = [self.multas objectAtIndex:indexPath.row];

        NSString *dataIsolada = [[NSString stringWithFormat:[multa dataOcorrencia]] substringWithRange:NSMakeRange(0, 10)];

        [[cell textLabel] setText:[NSString stringWithFormat:dataIsolada]];
        [[cell detailTextLabel] setText:[multa descricao]];
        [cell.imageView setImageWithURL:[NSURL URLWithString:[multa fotoURL]]
                       placeholderImage:[UIImage imageNamed:@"carregando.jpeg"]];
        [cell.imageView setContentMode: UIViewContentModeScaleAspectFit];
        [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    }

    return cell;
}

当用户点击公开按钮时,我切换活动指示器的公开按钮,然后使用导航控制器将另一个视图推送到屏幕:

- (void)carregarDetalhesMulta:(UITableView *)tableView comMulta:(Multa *)multa 
{
    DetalheMultaViewController *form = [[DetalheMultaViewController alloc] initWithMulta:multa];
    form.delegate = self;

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:form];

    [self presentModalViewController:navigation animated:YES];
}

- (void) tableView: (UITableView *) tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *) indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
    spinner.frame = CGRectMake(0, 0, 24, 24);
    cell.accessoryView = spinner;
    [spinner startAnimating];

    Multa *multa = [self.multas objectAtIndex:indexPath.row];

    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

       [spinner stopAnimating];
       [self carregarDetalhesMulta:tableView comMulta:multa];
       [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    });

}

请注意,我最后更改了配件的公开按钮。问题是:当我回来(通过点击按钮返回)到包含UITableView的视图时,我点击的行显示没有公开按钮或活动指示器。我做错了什么?在不重新加载表视图的情况下切换这些控件的更好方法是什么?

2 个答案:

答案 0 :(得分:2)

accessoryView属性优先于accessoryType属性。如果您将accessoryView设置为nil,则会再次看到附件按钮。

答案 1 :(得分:0)

以下是我从问题中理解的内容:您希望附件视图成为公开按钮,在点按时会成为活动指示符。在短暂的延迟之后,您想要推送到新的视图控制器。

我将如何做到这一点:

  1. 在故事板中设置原型单元以使附件:无。
  2. 从视图控制器添加一个push segue到你想要的目标vc,给它一个标识符,比如@“MyPushSegue”
  3. 在cellForRowAtIndexPath中将accessoryView添加到单元格(如果它们没有):

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        // the rest of your cellForRowAtIndexPath
    
         if (!cell.accessoryView)
            cell.accessoryView = [self createAccessoryView];
    
        return cell;
    }
    

    以这种方式创建配件视图(假设为ARC):

    - (UIView *)createAccessoryView {
    
        UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectZero];
        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
        disclosureButton.tag = 100;
        [disclosureButton addTarget:self action:@selector(pressedAccessory:) forControlEvents:UIControlEventTouchUpInside];
    
        UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        aiv.tag = 101;
        aiv.alpha = 0.0;
        aiv.center = disclosureButton.center;
    
        accessoryView.bounds = disclosureButton.bounds;
        [accessoryView addSubview:aiv];
        [accessoryView addSubview:disclosureButton];
        return accessoryView;
    }
    

    上面的公开按钮在按下按钮时触发方法。此方法应更改选择器视图的状态并触发您在故事板中设置的推送segue:

    - (void)pressedAccessory:(UIButton *)sender {
    
        UIView *accessoryView = sender.superview;
        [self setAccessoryView:accessoryView waiting:YES];
    
        // let the user see the spinner for 1 seconds
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
            [self performSegueWithIdentifier:@"MyPushSegue" sender:self];
            [self setAccessoryView:accessoryView waiting:NO];
        });
    }
    

    最后,为了解决您遇到的问题,我们不会用微调器替换附件视图,我们通过隐藏/显示按钮并隐藏/显示微调器来改变它的外观,具体取决于我们想要的状态:

    - (void)setAccessoryView:(UIView *)accessoryView waiting:(BOOL)waiting {
    
        UIButton *disclosureButton = (UIButton *)[accessoryView viewWithTag:100];
        UIActivityIndicatorView *aiv = (UIActivityIndicatorView *)[accessoryView viewWithTag:101];
        [UIView animateWithDuration:0.1 animations:^{
            disclosureButton.alpha = (waiting)? 0.0 : 1.0;
            aiv.alpha = (waiting)? 1.0 : 0.0;
        }];
    
        if (waiting) [aiv startAnimating]; else [aiv stopAnimating];
    }
    
相关问题