我将分组的表格视图:
当第一部分的唯一一行与一个状态相关联时,比如A,我只能看到第一部分,可能还有一些文本(例如在页脚中);
当这个状态发生变化时,我会看到第一个下面的其他部分;
我怎么能实现这个目标?一些代码/链接获得类似的东西?
谢谢,
弗兰
答案 0 :(得分:1)
没问题,只需在所有tableView数据源和委托方法中添加一些if else逻辑。
例如:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!canUseInAppPurchase || isLoading) {
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!canUseInAppPurchase || isLoading) {
return 1;
}
if (section == 0) {
// this will be the restore purchases cell
return 1;
}
return [self.products count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = ...
NSString *cellText = nil;
if (!canUseInAppPurchase) {
cellText = @"Please activate inapp purchase";
}
else if (isLoading) {
cellText = @"Loading...";
}
else {
if (section == 0) {
cellText = @"Restore purchases";
}
else {
cellText = productName
}
}
cell.textLabel.text = cellText;
return cell;
}
如果你想添加或删除第二部分,你可以使用简单的[tableView reloadData];或者这个更平滑的变体:
[self.tableView beginUpdates];
if (myStateBool) {
// activated .. show section 1 and 2
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationTop];
}
else {
// deactivated .. hide section 1 and 2
[self.tableView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationBottom];
}
[self.tableView endUpdates];
小心,您必须先更改数据源中的数据。此代码将添加2个部分。但您可以轻松地将其用于满足您的需求。