简体   繁体   中英

iOS Set UITableView Expanded Initially

I have a UITableView which has expandable custom cells. Multiple cells can be expanded. For that I have an array expandedArray in which I store the indexPath of all cells that are expanded. Initially all cells are collapsed. I want in initial start all cells to be expanded. This is my cellForRowAtIndexPath code :-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//  VISITORS LISTS TV
NSUInteger count = 0;

VisitorsListsCell *vcell = (VisitorsListsCell *) [tableView dequeueReusableCellWithIdentifier:@"VisitorsListsCell"];

if (vcell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"VisitorsListsCell" owner:self options:nil];
    vcell = [nib objectAtIndex:0];
}
// Button - Round Corners
[vcell button].layer.cornerRadius = 5;
[vcell button].contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

NSString *text = [visistorsList objectAtIndex:indexPath.row];
// SET PROPERTIES OF CELL
// ....

if ([expandedArray containsObject:indexPath]) {
    // Do expanded cell stuff
    [vcell setButtonText:[visistorsList objectAtIndex:indexPath.row] withCount:(int)count isExpanded:YES];
    [vcell.button setImage:[UIImage imageNamed:@"arrowup.png"] forState:UIControlStateNormal ];
} else {
    [vcell setButtonText:[visistorsList objectAtIndex:indexPath.row] withCount:(int)count isExpanded:NO];
    [vcell.button setImage:[UIImage imageNamed:@"arrowdown.png"] forState:UIControlStateNormal ];
}

[[vcell listsTableView] reloadData];

return vcell;
}

I want is when the page is loaded, all cells are expanded. I believe for that I need to add indexPath of each cell to expandedArray. But on initial run only, how do I add the indexPath ??? I can't find a way to implement it.

Is it possible, any clue. OR any other way to achieve the goal. Any help is highly appreciated. Thanks.

To add indexPath to your expanded array you should use code below. But much simpler way is to use the same methods that are used in -numberOfSections and numberOfRowsInSection: . Using tableView delegates is not a good way.

- (void)viewDidLoad {
    self.expandedArray = [NSMutableArray new];
    for (int itSection=0; itSection<[self.tableView numberOfSections]; itSection++) {
        for (int itRow=0; itRow<[self.tableView numberOfRowsInSection:itSection]; itRow++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:itRow inSection:itSection];
            [self.expandedArray addObject:indexPath];
        }
    }
}

Btw: the correct way to implement cell from xib:

- (void)viewDidLoad {
    ....
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([VisitorsListsCell class]) bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"VisitorsListsCell"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM