gpt4 book ai didi

ios - 如何向 UITableView 添加部分?

转载 作者:行者123 更新时间:2023-11-29 13:14:20 26 4
gpt4 key购买 nike

有人可以用基本术语解释添加部分的流程是如何工作的吗?

我有一个对象数组,我目前正在将这些对象填充到单个分段 UITableView 中,但是我想根据这些对象的共享“类别”属性将它们分成多个部分。我正在从 API 获取对象列表,所以我事先不知道我将拥有多少个类别。

非常非常感谢。

最佳答案

您必须使用 UITableViewDataSource 协议(protocol)。您必须实现其中一种可选方法:

//Optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Default is 1 if not implemented

NSUInteger sectionCount = 5; //Or whatever your sections are counted as...
return sectionCount;
}

确保每个部分都计算你的行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4; //This will return 4 rows in each section
}

如果您想为每个部分命名页眉和页脚:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"";
}// fixed font style. use custom view (UILabel) if you want something different

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"";
}

最后,确保正确实现单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

NSUInteger section = [indexPath indexAtPosition:0];
NSUInteger rowInSection = [indexPath indexAtPosition:1];

//Do your logic goodness here grabbing the data and creating a reusable cell

return nil; //We would make a cell and return it here, which corresponds to that section and row.
}

可折叠部分完全是另一种野兽。您需要对 UITableView 进行子类化,或者只在 CocoaControls 上找到一个。

关于ios - 如何向 UITableView 添加部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205740/

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