显示数据的可扩展 UITableView 单元格
Expandable UITableView cells showing datas
我有一些按年份分类的体育比赛,对于每场比赛,我都有最终结果、比赛日期和得分手。
我在 'Table View' 中显示这些匹配项,如下所示:
所以我想实现的是:当点击一个单元格时,显示如图所示的匹配细节。
我也找到了一些库来实现 accordion/expandable 风格,但没有人这样做。他们只是展开单元格并显示另一个单元格。
在这种情况下,您甚至不需要使用 expandable/accordion。这是解决此问题的方法。假设您的单元格大小在正常时为 40,在特定点击时为 100。在 heightForRowAtIndexPath 中,您可以检查选择了哪个单元格和 return 更多高度
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];
因此,您的单元格将全部呈现,但基于您隐藏或显示内容的高度。
输入来源更新答案
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];
}
}
更简洁的方法是创建 2 个具有不同标识符的单元格,并在您单击该单元格时 运行 更改它。检查这个问题:
您可以为此目的使用 HVtableview,试试这个 link
https://github.com/xerxes235/HVTableView
尝试以下方法之一:
- obj-C: KMAccordionTableViewController
- swift: AccordionTableViewController
我有一些按年份分类的体育比赛,对于每场比赛,我都有最终结果、比赛日期和得分手。
我在 'Table View' 中显示这些匹配项,如下所示:
所以我想实现的是:当点击一个单元格时,显示如图所示的匹配细节。
我也找到了一些库来实现 accordion/expandable 风格,但没有人这样做。他们只是展开单元格并显示另一个单元格。
在这种情况下,您甚至不需要使用 expandable/accordion。这是解决此问题的方法。假设您的单元格大小在正常时为 40,在特定点击时为 100。在 heightForRowAtIndexPath 中,您可以检查选择了哪个单元格和 return 更多高度
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];
因此,您的单元格将全部呈现,但基于您隐藏或显示内容的高度。
输入来源更新答案
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];
}
}
更简洁的方法是创建 2 个具有不同标识符的单元格,并在您单击该单元格时 运行 更改它。检查这个问题:
您可以为此目的使用 HVtableview,试试这个 link https://github.com/xerxes235/HVTableView
尝试以下方法之一:
- obj-C: KMAccordionTableViewController
- swift: AccordionTableViewController