gpt4 book ai didi

iOS 设置 UITableView 初始扩展

转载 作者:行者123 更新时间:2023-11-29 02:45:48 24 4
gpt4 key购买 nike

我有一个 UITableView,它具有可扩展的自定义单元格。可以展开多个单元格。为此,我有一个数组 ExpandedArray,其中存储所有展开单元格的索引路径。最初,所有单元格都折叠起来。我希望在初始启动时扩展所有单元格。这是我的 cellForRowAtIndexPath 代码:-

- (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;
}

我想要的是当页面加载时,所有单元格都会展开。我相信我需要将每个单元格的indexPath 添加到expandedArray 中。但仅在初次运行时,如何添加索引路径???我找不到实现它的方法。

是否可能,任何线索。或者任何其他方式来实现目标。非常感谢任何帮助。谢谢。

最佳答案

要将 indexPath 添加到扩展数组中,您应该使用下面的代码。但更简单的方法是使用在 -numberOfSectionsnumberOfRowsInSection: 中使用的相同方法。使用 tableView 委托(delegate)不是一个好方法。

- (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];
}
}
}

顺便说一句:从 xib 实现单元格的正确方法:

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

关于iOS 设置 UITableView 初始扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com