ios - 如何在 UITableView 中创建行的展开和折叠
问题描述
如何在 UITableView 中创建行的展开和折叠,
单击单元格后,我关注了此视频“ https://www.youtube.com/watch?v=OYaI5ASpsOE ”,出现以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to compare <NSIndexPath: 0x600000029560> {length = 2, path = 0 - 18446744073709551615} with object of different class: NSArray'
解决方案
在我使用崩溃的 tableview 委托和数据源代码下面对我来说是不可重现的
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrowtitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ExpandingCell *cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"Expanding Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblrow.text = self.arrow[indexPath.row];
cell.lblrowtitle.text = self.arrowtitle[indexPath.row];
cell.lblfruit.text = self.arrFruit[indexPath.row];
NSInteger cal = (indexPath.row) * 25;
cell.lblcalcal.text = [NSString stringWithFormat:@"%ld",(long) cal];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return (self.selectedIndex == indexPath.row) ? 120 : 45 ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedIndex == indexPath.row) {
self.selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if (self.selectedIndex == -1) {
NSIndexPath *prev = [NSIndexPath indexPathForRow:self.selectedIndex inSection:0];
self.selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:@[prev] withRowAnimation:UITableViewRowAnimationFade];
}
self.selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
推荐阅读
- javascript - 地图多次返回数组的项目
- python - 将第二个图表添加到 Dash app.layout 的问题
- python - 为什么 QTreeview 项目类在拖放事件后发生了变化?
- python - Python/Django '类型对象不可迭代' Django Rest Framework (DRF)
- vue.js - 如果有人点击太快,如何防止不必要的点击?
- calculator - 在 Pascal 中使用大小写的添加菜单
- postgresql - 只读事务异常
- amazon-web-services - aws 凭据错误无法加载凭据
- android - 片段中的 ViewBinding 属性是否应该始终具有支持的不可为空的属性?
- email - 为什么在 ARC 检查转发的电子邮件后,DMARC auth_results 仍然报告 dkim 失败?