BlueBear
BlueBear

Reputation: 7639

Add detail disclosure to UITableViewCell upon action?

I have a UITableView filled with cells that contain collection views. I am needing to press a button in my Navigation bar that will then add a detail disclosure button to all of my cells. This should then enable me to tap that and push me to a new view controller.

Is there a way to animate this action onto all of my table view cells so that a user can either show or hide that functionality at the tap of a button?

Upvotes: 0

Views: 1331

Answers (2)

Ahmet Kazim Günay
Ahmet Kazim Günay

Reputation: 1062

Not Exactly what u looking for but logic will be like this:

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (strong, nonatomic) NSMutableArray *checksArr;

@end

And In your YourViewController.m file

@implementation YourViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.checksArr = [[NSMutableArray alloc] init];

    for (int i=0; i<self.dataArr.count; i++) {
        [self.checksArr addObject:[NSNumber numberWithBool:NO]];
    }
}


#pragma mark - TableView Datasource
-(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];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
        if([[self.checksArr objectAtIndex:indexPath.row] boolValue]) {
           [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
        }

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArr count];
}


-(void) btnNavigationBarTapped:(id)sender {
    for (int i=0; i<self.dataArr.count; i++) {
            [self.checksArr replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:YES]];
        }
    [self.tableview reloadData];
}

Upvotes: 1

Zev Eisenberg
Zev Eisenberg

Reputation: 8158

Assuming you want to show/hide custom disclosure indicators on each of your UICollectionViewCell instances:

In your collectionView:cellForItemAtIndexPath: method, configure your cells to either show or hide the disclosure indicator based on some state variable, so that new cells that get configured as the result of scrolling will have the correct state:

MyCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];

// some variable that stores whether to show the indicators
cell.disclosureVisible = self.disclosureIndicatorsVisible;

// Any more setup you need to do with your cell

Then, when you tap the button to change the visibility of the indicator, you have a few options, depending on what you are trying to do:

  1. Call reloadData on the table view, which should cause all the collection views to refresh.
  2. For each visible table view cell, which you can get by calling [self.tableView indexPathsForVisibleRows], get each collection view cell with [cell.collectionView indexPathsForVisibleItems]. Then call a custom method on each cell which performs your animation to show or hide the indicator.

Edit: you said in your comment that you want the table view cells to show the detail disclosure indicator. In that case, you probably want to get all the visible cells of the table view as described above and set their accessoryType to UITableViewCellAccessoryDetailDisclosureButton. However, accessoryType is not an animatable property, so you might need to add a custom property to a UITableViewCell subclass that animates showing and hiding of a custom accessory view, which then responds to taps.

Upvotes: 0

Related Questions