在动作时向UITableViewCell添加细节披露



我有一个UITableView充满了包含集合视图的单元格。我需要按下一个按钮在我的导航栏,然后将详细信息披露按钮添加到我的所有单元格。这应该能让我点击那个并把我推送到一个新的视图控制器。

是否有一种方法可以将这个动作动画到我所有的表视图单元格上,以便用户可以在点击按钮时显示或隐藏该功能?

不完全是你想要的,但逻辑将是这样的:

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (strong, nonatomic) NSMutableArray *checksArr;
@end

和在你的YourViewController。

文件
@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];
}

假设您想要在每个UICollectionViewCell实例上显示/隐藏自定义披露指示器:

collectionView:cellForItemAtIndexPath:方法中,将单元格配置为显示或隐藏基于某些状态变量的披露指示器,以便作为滚动结果配置的新单元格将具有正确的状态:

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

然后,当你点击按钮来改变指示器的可见性时,你有几个选项,这取决于你想做什么:

  1. 在表视图上调用reloadData,这将导致所有的集合视图刷新。
  2. 对于每个可见的表视图单元格,您可以通过调用[self.tableView indexPathsForVisibleRows]获得,使用[cell.collectionView indexPathsForVisibleItems]获得每个集合视图单元格。然后在每个执行动画的单元格上调用自定义方法来显示或隐藏指示器。

编辑:您在评论中说您希望表视图单元格显示详细信息披露指示器。在这种情况下,您可能希望如上所述获得表视图的所有可见单元格,并将其accessoryType设置为UITableViewCellAccessoryDetailDisclosureButton。然而,accessoryType不是一个可动画的属性,所以你可能需要添加一个自定义属性到UITableViewCell子类中,这个子类可以动画化自定义附件视图的显示和隐藏,然后对点击做出响应。

最新更新