我试图用以下结构实现一个uitableview:
- section 1
- cell 0
- cell 1
- cell 2
- section 1
- cell 0
- cell 1
- cell 2
它应该像这个截图(1-2-3-4)那样责骂:http://dl.dropbox.com/u/2213241/uitableview.png。
因此,两个部分总是可见的。
我如何实现这一点?或者已经有人实现了这一点?
谢谢:)
发布于 2014-09-18 13:01:29
有嵌套部分的诀窍是在表视图中有两种类型的行。一个表示第二级别的节,另一个表示表视图中的正常行。假设您有一个两级数组(例如区段)来表示表视图中的项。
那么,我们所拥有的区段总数就是顶级部分的数量。每个顶层区段中的行数为子节数+每个分节中的行数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *sectionItems = self.sections[(NSUInteger) section];
NSUInteger numberOfRows = sectionItems.count; // For second level section headers
for (NSArray *rowItems in sectionItems) {
numberOfRows += rowItems.count; // For actual table rows
}
return numberOfRows;
}现在,我们需要考虑的就是如何为表视图创建行。在情节提要中设置两个具有不同重用标识符的原型,一个用于节标题,另一个用于行项,并根据数据源方法中的询问索引实例化正确的原型。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
NSMutableArray *sectionHeaders = self.sectionHeaders[(NSUInteger) indexPath.section];
NSIndexPath *itemAndSubsectionIndex = [self computeItemAndSubsectionIndexForIndexPath:indexPath];
NSUInteger subsectionIndex = (NSUInteger) itemAndSubsectionIndex.section;
NSInteger itemIndex = itemAndSubsectionIndex.row;
if (itemIndex < 0) {
// Section header
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SECTION_HEADER_CELL" forIndexPath:indexPath];
cell.textLabel.text = sectionHeaders[subsectionIndex];
return cell;
} else {
// Row Item
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ROW_CONTENT_CELL" forIndexPath:indexPath];
cell.textLabel.text = sectionItems[subsectionIndex][itemIndex];
return cell;
}
}
- (NSIndexPath *)computeItemAndSubsectionIndexForIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
NSInteger itemIndex = indexPath.row;
NSUInteger subsectionIndex = 0;
for (NSUInteger i = 0; i < sectionItems.count; ++i) {
// First row for each section item is header
--itemIndex;
// Check if the item index is within this subsection's items
NSArray *subsectionItems = sectionItems[i];
if (itemIndex < (NSInteger) subsectionItems.count) {
subsectionIndex = i;
break;
} else {
itemIndex -= subsectionItems.count;
}
}
return [NSIndexPath indexPathForRow:itemIndex inSection:subsectionIndex];
}这是一篇详细的文章介绍了如何做到这一点。
发布于 2018-11-13 13:44:49
如果有人对上述代码的SWIFT4.2版本感兴趣,请看这里。
为了有嵌套的部分,您需要在tableView中有几种类型的行。首先表示第二个级别的节,第二个表示tableView中的标准行。假设您有一个两级数组(节)来表示tableView中的项。
那么,我们所拥有的部分的总数就是顶级部分的数量。每个顶层部分中的行数将是子节的数目+每个小节中的行数。
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionItems = sections[section]
var numberOfRows: Int = sectionItems.count // For second level section headers
for rowItems: [Any] in sectionItems as? [[Any]] ?? [] {
numberOfRows += rowItems.count // For actual table rows
}
return numberOfRows
}现在,您需要考虑的就是如何为tableView创建行。在情节提要中设置两个具有不同重用标识符的原型,一个用于节标题,另一个用于行项,并根据数据源方法中的询问索引实例化正确的原型。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var sectionItems = sections[indexPath.section]
var sectionHeaders = self.sectionHeaders[indexPath.section]
let itemAndSubsectionIndex: IndexPath? = computeItemAndSubsectionIndex(for: indexPath)
let subsectionIndex = Int(itemAndSubsectionIndex?.section ?? 0)
let itemIndex: Int? = itemAndSubsectionIndex?.row
if (itemIndex ?? 0) < 0 {
// Section header
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "SECTION_HEADER_CELL", for: indexPath)
cell.textLabel?.text = sectionHeaders[subsectionIndex] as? String
return cell
} else {
// Row Item
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ROW_CONTENT_CELL", for: indexPath)
cell.textLabel?.text = sectionItems[subsectionIndex][itemIndex ?? 0] as? String
return cell
}
}
func computeItemAndSubsectionIndex(for indexPath: IndexPath?) -> IndexPath? {
var sectionItems = sections[Int(indexPath?.section ?? 0)]
var itemIndex: Int? = indexPath?.row
var subsectionIndex: Int = 0
for i in 0..<sectionItems.count {
// First row for each section item is header
itemIndex = (itemIndex ?? 0) - 1
// Check if the item index is within this subsection's items
let subsectionItems = sectionItems[i] as? [Any]
if (itemIndex ?? 0) < Int(subsectionItems?.count ?? 0) {
subsectionIndex = i
break
} else {
itemIndex -= subsectionItems?.count
}
}
return IndexPath(row: itemIndex ?? 0, section: subsectionIndex)
}发布于 2011-10-04 16:31:56
像罗宾建议的那样,子类UITableView将是一个很好的选择。我自己也做过类似的事情,但是我对UITableViewCell进行了子类化,并在其中放置了UITableView。在这种情况下,您将有一个基本的tableView,其中每个部分都是一个组。因为子类UITableViewCell,所以每一行都是它自己的UITableView,它有自己的节和行。这应该会给你你想要的外观和功能。如果你有困难的话,我很乐意帮你把它设置好,但这并不难。本教程给出了一个很好的示例,说明如何对UITableViewCell进行子类,并且是一个很好的起点。
https://stackoverflow.com/questions/7650243
复制相似问题