Last active
March 3, 2017 06:23
-
-
Save Amztion/d1bfca691d589397e886 to your computer and use it in GitHub Desktop.
在点击 UITableViewCell 时展开、收缩表格
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self.tableView setAllowsMultipleSelection:YES]; //允许选择多行 | |
} | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if ([tableView indexPathsForSelectedRows].count) { | |
//indexPathsForSelectedRows 类型为 NSArray<NSIndexPath>,存储了所有被选择的列的indexPath,如果为空则没有已经被选择的 | |
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) { | |
//如果当前点击的列的 inexPath 没有在 indexPathsForSelectedRows 中找到,则说明当前列没有被点击,则展开;如果找到了,说明正在展开状态 | |
return expandedHeight; // 展开后的表格高度 | |
} | |
return contractedHeight; // 正常高度 | |
} | |
return contractedHeight; // 正常高度 | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[self updateTableView]; //点击一次选择中表格,该行的 indexPath 存入 indexPathsForSelectedRows,该行高度变大 | |
} | |
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[self updateTableView]; //再点击一次取消选择表格,该行 indexPath 从 indexPathsForSelectedRows 中删除,高度恢复正常 | |
} | |
- (void)updateTableView //更新表格 | |
{ | |
[self.tableView beginUpdates]; | |
[self.tableView endUpdates]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment