显示数据的可扩展 UITableView 单元格 [英] Expandable UITableView cells showing datas

查看:34
本文介绍了显示数据的可扩展 UITableView 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些按年份分类的体育比赛,每场比赛都有最终结果、比赛日期和得分手.

I have some sport matches cataloged by year, and for every match I have the final result, the match date and the scorer(s).

我在表格视图"中显示这些匹配项,如下所示:

I'm showing those matches in a 'Table View', like this:

所以我想要实现的是:当点击一个单元格时,显示如图所示的匹配细节.

So what I'd like to achieve is: when clicking on a cell, show the match details as shown in the picture.

我还找到了一些库来实现手风琴/可扩展风格,但没有人能做到这一点.他们只是展开单元格并显示另一个单元格.

I also found some libraries to achieve the accordion/expandable style, but no-one does the job. They just expand the cell and show another cell.

推荐答案

在这种情况下,您甚至不需要使用 expandable/accordion.这是您可以解决的方法.让我们说正常时的单元格大小为 40,特定单击时为 100.在 heightForRowAtIndexPath 中,您可以检查选择了哪个单元格并返回更多高度

You don't even need to use expandable/accordion in this case. Here is how you can tackle this. Lets says your cell size when normal is 40 and when particular clicked is 100. In heightForRowAtIndexPath you can check which cell is selected and return more height

if(selectedRow == indexPath.row) {
    return 100;
} else {
    return 40;
}

然后你可以做的是在 didSelectRowAtIndexPath 或 clickEvent 方法中

Then what you can do is in didSelectRowAtIndexPath or a clickEvent method

[self.tableView beginUpdates];
[[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: selectedRow inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

因此,您的单元格将全部呈现,但基于您隐藏或显示内容的高度.

So it will be your cell will be rendered all of it but based on height you are hiding or showing content.

输入来源更新答案

ViewController.m

ViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(selectedIndex == indexPath.row)
    return 100;
else
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSDictionary *matches = self.dataArray[indexPath.row];
    cell.matchName.text = [matches objectForKey@"MatchName"];
    if() {
        cell.heightConstraintSecondView.constant = 0;
    } else {
        cell.heightConstraintSecondView.constant = 59;
        cell.team1.text = [matches objectForKey@"Team1"];
        cell.team2.text = [matches objectForKey@"Team2"];
        cell.score.text = [matches objectForKey@"Score"];
    } 
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndex = indexPath.row;
    [self.tableView beginUpdates];
    [[self tableView] reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    }
}

这篇关于显示数据的可扩展 UITableView 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆