390 cocoa-touch uitableview ios
我UITableView在我的iPhone应用程序中使用了一个,我有一个属于一个组的人员列表.我希望这样当用户点击特定的人(从而选择单元格)时,单元格的高度会增加,以显示多个UI控件来编辑该人的属性.
这可能吗?
Sim*_*Lee 876
我找到了一个非常简单的解决方案作为UITableView我正在努力的副作用.....
将单元格高度存储在通常通过其报告原始高度的变量中tableView: heightForRowAtIndexPath:,然后当您想要设置高度变化动画时,只需更改变量的值并调用此...
[tableView beginUpdates];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
你会发现它没有完全重新加载,但足以UITableView知道它必须重绘单元格,抓住单元格的新高度值....并猜猜是什么?它为您改变了动态.甜.
我在我的博客上有更详细的解释和完整的代码示例... Animate UITableView Cell Height Change
Mar*_* A. 61
我喜欢Simon Lee的回答.我实际上并没有尝试那种方法,但看起来它会改变列表中所有单元格的大小.我希望改变被挖掘的细胞.我有点喜欢西蒙,但差别很小.这将在选择单元格时更改其外观.它确实有动画.只是另一种方式来做到这一点.
创建一个int来保存当前所选单元格索引的值:
int currentSelection;
Run Code Online (Sandbox Code Playgroud)
然后:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int row = [indexPath row];
selectedNumber = row;
[tableView beginUpdates];
[tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
然后:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == currentSelection) {
return 80;
}
else return 40;
}
Run Code Online (Sandbox Code Playgroud)
我相信你可以在tableView中进行类似的更改:cellForRowAtIndexPath:改变单元格的类型,甚至加载单元格的xib文件.
像这样,currentSelection将从0开始.如果您不希望默认情况下选择列表的第一个单元格(在索引0处),则需要进行调整.
小智 22
添加属性以跟踪所选单元格
@property (nonatomic) int currentSelection;
Run Code Online (Sandbox Code Playgroud)
将其设置为(例如)中的标记值viewDidLoad,以确保UITableView在"正常"位置开始
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//sentinel
self.currentSelection = -1;
}
Run Code Online (Sandbox Code Playgroud)
在heightForRowAtIndexPath您可以设置所选单元格所需的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = self.newCellHeight;
} else rowHeight = 57.0f;
return rowHeight;
}
Run Code Online (Sandbox Code Playgroud)
在didSelectRowAtIndexPath您保存当前选择并保存动态高度(如果需要)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// set selection
self.currentSelection = indexPath.row;
// save height for full text label
self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}
Run Code Online (Sandbox Code Playgroud)
在didDeselectRowAtIndexPath将选择索引设置回sentinel值并将单元格设置为正常形式
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// sentinel
self.currentSelection = -1;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}
Run Code Online (Sandbox Code Playgroud)
raf*_*raf 15
而不是beginUpdates()/ endUpdates(),现在推荐的调用是:
tableView.performBatchUpdates(nil, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Apple 说,关于 beginUpdates/endUpdates:“尽可能使用 performBatchUpdates(_:completion:) 方法而不是这个方法。”
请参阅:https : //developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates
law*_*nce 14
reloadData不好,因为没有动画......
这就是我目前正在尝试的:
NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
它几乎正常.几乎.我正在增加单元格的高度,有时在表格视图中有一点"打嗝",因为单元格被替换,好像保存了表格视图中的某些滚动位置,新单元格(这是第一个单元格)在表中)最终其偏移量太高,并且滚动视图反弹以重新定位它.
fer*_*min 10
我解决了reloadRowsAtIndexPaths.
我保存在didSelectRowAtIndexPath所选单元格的indexPath中并reloadRowsAtIndexPaths在最后调用(您可以发送NSMutableArray以获取要重新加载的元素列表).
在heightForRowAtIndexPath您可以检查是否indexPath在列表与否的expandIndexPath细胞的和发送的高度.
您可以查看以下基本示例:https: //github.com/ferminhg/iOS-Examples/tree/master/iOS-UITableView-Cell-Height-Change/celdascambiadetam 这是一个简单的解决方案.
如果有帮助,我会添加一些代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
{
if ([indexPath isEqual:_expandIndexPath])
return 80;
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Celda";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.textLabel setText:@"wopwop"];
return cell;
}
#pragma mark -
#pragma mark Tableview Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *modifiedRows = [NSMutableArray array];
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
_expandIndexPath = indexPath;
[modifiedRows addObject:indexPath];
// This will animate updating the row sizes
[tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
Run Code Online (Sandbox Code Playgroud)
Swift 4 及以上
将以下代码添加到 tableview 的 didselect 行委托方法中
tableView.beginUpdates()
tableView.setNeedsLayout()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)
los*_*sit -2
获取所选行的索引路径。重新加载表。在 UITableViewDelegate 的 heightForRowAtIndexPath 方法中,将所选行的高度设置为不同的高度,而对于其他行则返回正常的行高
| 归档时间: |
|
| 查看次数: |
124248 次 |
| 最近记录: |