【发布时间】:2011-02-20 17:25:24
【问题描述】:
我想要一个分组的表格视图,这样:
-
1234563
当这个状态改变时,我会在第一个下面看到其他部分;
我怎样才能做到这一点?一些代码/链接来获得类似的东西?
谢谢,
弗兰
【问题讨论】:
标签: iphone uitableview sections
我想要一个分组的表格视图,这样:
当这个状态改变时,我会在第一个下面看到其他部分;
我怎样才能做到这一点?一些代码/链接来获得类似的东西?
谢谢,
弗兰
【问题讨论】:
标签: iphone uitableview sections
没问题,只需在所有 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 个部分。但是您可以轻松地根据自己的需要采用它。
【讨论】: