OGeek|极客世界-中国程序员成长平台

标题: ios - 如何创建可扩展的 UITableView 部分? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:33
标题: ios - 如何创建可扩展的 UITableView 部分?

目前,我的每个 UITableView 部分都包含 10 行。我想要的是每个部分初始化这 10 行,但只显示 3。如果用户点击“显示更多”按钮,那么将显示所有 10。我考虑过在隐藏部分使用类似的东西来做到这一点:

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{    
    float heightForRow = 65;

    if(cell.tag>=3)
        return 0;
    else
        return heightForRow;
}

这是为了隐藏除前 3 行之外的所有行。问题是我收到一条错误消息,指出 use of undeclared identifier 'cell'。我像这样在 cellForRowAtIndexPath 中初始化 cell:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // No cell seperators = clean design
    tableView.separatorColor = [UIColor clearColor];

    // title of the item
    cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"rice"]];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];

    return cell;

}

如何使 cellheightForRowAtIndexPath 中可访问?



Best Answer-推荐答案


如果您可以在这里使用 3rd Party Libraries,那么您就可以了:

SLExpandableTableView

实现步骤

UITableViewController

中加载 SLExpandableTableView
- (void)loadView
{
    self.tableView = [[SLExpandableTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
}

实现 SLExpandableTableViewDatasource 协议(protocol)

- (BOOL)tableViewSLExpandableTableView *)tableView canExpandSectionNSInteger)section
{
    // return YES, if the section should be expandable
}

- (BOOL)tableViewSLExpandableTableView *)tableView needsToDownloadDataForExpandableSectionNSInteger)section
{
    // return YES, if you need to download data to expand this section. tableView will call tableView:downloadDataForExpandableSection: for this section
}

- (UITableViewCell<UIExpandingTableViewCell> *)tableViewSLExpandableTableView *)tableView expandingCellForSectionNSInteger)section
{
    // this cell will be displayed at IndexPath with section: section and row 0
}

实现 SLExpandableTableViewDelegate 协议(protocol)

- (void)tableView:(SLExpandableTableView *)tableView downloadDataForExpandableSection:(NSInteger)section
{
    // download your data here
    // call [tableView expandSection:section animated:YES]; if download was successful
    // call [tableView cancelDownloadInSection:section]; if your download was NOT successful
}

- (void)tableView:(SLExpandableTableView *)tableView didExpandSection:(NSUInteger)section animated:(BOOL)animated
{
  //...
}

- (void)tableView:(SLExpandableTableView *)tableView didCollapseSection:(NSUInteger)section animated:(BOOL)animated
{
  //...
}

关于ios - 如何创建可扩展的 UITableView 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186109/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4