我有一些按年份编录的体育比赛,对于每场比赛,我都有最终结果、比赛日期和得分手。
我在“表格视图”中显示这些匹配项,如下所示:
所以我想要实现的是:当单击单元格时,显示比赛详细信息,如图所示。
我还找到了一些库来实现手风琴/可扩展风格,但没有人能完成这项工作。他们只是展开单元格并显示另一个单元格。
在这种情况下,您甚至不需要使用可扩展/手风琴。以下是解决这个问题的方法。假设您的单元格大小正常时为 40,特定单击时为 100。在 heightForRowAtIndexPath 中,您可以检查选择了哪个单元格并返回更多高度
if(selectedRow == indexPath.row) {
return 100;
} else {
return 40;
}
那么你可以做的是在 didSelectRowAtIndexPath 或 clickEvent 方法中
[self.tableView beginUpdates];
[[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: selectedRow inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
因此,您的单元格将被渲染所有内容,但基于您隐藏或显示内容的高度。
输入源更新了答案
视图控制器.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];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)