Tar*_*riq 55 iphone objective-c uibutton uitableview
让我们说我们有一个自定义UITableViewCell
因此,每当我单击单元格上的自定义按钮时,它应该扩展到某种程度(你可以说更多40个高度......)当我再次点击相同的自定义按钮时,它应该折叠到之前的高度.
开发者请指导我..我怎样才能完成这项任务
Mic*_*lum 94
考虑到这是完全正确的,我不打算在这里说任何与被接受的答案相矛盾的事情.但是,我将详细介绍如何实现这一目标.如果您不想阅读所有这些内容并且对使用工作项目中的源代码更感兴趣,我已经将示例项目上传到GitHub.
基本思想是在方法内部具有一个条件,该条件-tableView: heightForRowAtIndexPath:确定是否应该扩展当前单元格.这将通过从内部调用表上的开始/结束更新来触发.-tableView: didSelectRowAtIndexPath:在此示例中,我将展示如何创建允许一次扩展一个单元的表视图.
您需要做的第一件事是声明对NSIndexPath对象的引用.您可以随意执行此操作,但我建议使用如下属性声明:
@property (strong, nonatomic) NSIndexPath *expandedIndexPath;
Run Code Online (Sandbox Code Playgroud)
注意:您无需在viewDidLoad或任何其他类似方法中创建此索引路径.索引最初为零的事实只意味着表最初不会有扩展行.如果您希望表格开始时展开您选择的行,您可以在viewDidLoad方法中添加以下内容:
NSInteger row = 1;
NSInteger section = 2;
self.expandedIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
Run Code Online (Sandbox Code Playgroud)
下一步是转到您的UITableViewDelegate方法-tableView: didSelectRowAtIndexPath:,根据用户选择添加逻辑以更改扩展的单元格索引.这里的想法是检查刚刚根据存储在expandedIndexPath变量中的索引路径选择的索引路径.如果两者匹配,那么我们知道用户正在尝试取消选择扩展单元格,在这种情况下,我们将变量设置为nil.否则,我们将expandedIndexPath变量设置为刚刚选择的索引.这是在对beginUpdates/endUpdates的调用之间完成的,以允许表视图自动处理过渡动画.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates]; // tell the table you're about to start making changes
// If the index path of the currently expanded cell is the same as the index that
// has just been tapped set the expanded index to nil so that there aren't any
// expanded cells, otherwise, set the expanded index to the index that has just
// been selected.
if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
self.expandedIndexPath = nil;
} else {
self.expandedIndexPath = indexPath;
}
[tableView endUpdates]; // tell the table you're done making your changes
}
Run Code Online (Sandbox Code Playgroud)
然后最后一步是在另一个UITableViewDelegate方法中-tableView: heightForRowAtIndexPath:.在beginUpdates为表确定需要更新的每个索引路径触发一次之后,将调用此方法.您可以在此处比较expandedIndexPath当前正在重新评估的索引路径.
如果两个索引路径相同,那么这是您希望展开的单元格,否则它的高度应该是正常的.我使用了值100和44,但您可以使用适合您需求的值.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Compares the index path for the current cell to the index path stored in the expanded
// index path variable. If the two match, return a height of 100 points, otherwise return
// a height of 44 points.
if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
return 100.0; // Expanded height
}
return 44.0; // Normal height
}
Run Code Online (Sandbox Code Playgroud)
kri*_*ris 68
实现heightForRowAtIndexPath以计算正确的高度.然后在按钮的代码中,强制表格使用beginUpdates和endUpdates重新评估每个单元格的高度:
[self.tableView beginUpdates];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
使用heightForRowAtIndexPath自动计算对tableview单元格高度的更改,并且还会对更改进行动画处理.
实际上,您可能只是选择单元格来执行此操作,而不是单元格上的按钮didSelectRowAtIndexPath.
而不是使用[tableView beginUpdates]和[tableView endUpdates],我正在使用[tableView reloadRowsAtIndexPath:... withRowAnimation:...]方法内部的didSelectRowAtIndexPath方法.
我更喜欢这个,因为UITableViewCell当我使用开始和结束更新方法扩展时,我会遇到一些应该显示的元素问题.另一点是你可以选择一些动画,如:上,下,左,右......
| 归档时间: |
|
| 查看次数: |
54264 次 |
| 最近记录: |