• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何创建可扩展的 UITableView 部分?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:33:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

目前,我的每个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap