2013-04-02 52 views
1

我有一個UITableView,它具有包含圖像,文本和泄漏按鈕的單元。我加載的細胞是這樣的:UITableViewCellAccessoryDe​​tailDisclosureButton在更改UITableViewCell附件後消失

- (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的視圖)時,我挖掘的那一行顯示沒有泄露按鈕或活動指示器。我做錯了什麼?什麼是更好的方式來切換這些控件,而無需重新加載表視圖?

+1

make cell.accessoryView = NULL;然後使用你的代碼 –

回答

2

accessoryView資源在accessoryType屬性具有優先權。如果您將accessoryView設置爲nil,您將再次看到附件按鈕。

+0

非常感謝。 – gabsferreira

0

以下是我從問題中瞭解到的情況:您希望附件視圖是一個披露按鈕,當點擊該按鈕時,該按鈕將成爲活動指示器。在短暫的延遲後,你想推到一個新的視圖控制器。

這是我會怎麼做:

  1. 設置在故事板原型細胞有附件:無。
  2. 從您的視圖控制器中的cellForRowAtIndexPath加推SEGUE到你想要的目的地VC,給它一個標識符,說@「MyPushSegue」

的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]; 
} 
+0

也許這樣做,我會實現我所需要的,但現在斯科特的答案解決了我的問題。稍後當我不着急時,我會嘗試這種方法。謝謝。 – gabsferreira

相關問題