单击另一个单元格时,将UITableViewCell折叠为原始大小 [英] Collapse UITableViewCell to original size when another cell is clicked
本文介绍了单击另一个单元格时,将UITableViewCell折叠为原始大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
This question is connected to the following question: Can you animate a height change on a UITableViewCell when selected?
我通过使用以下代码来动画化该问题中说明的UITableViewCell的高度变化:
I am animating the height change of the UITableViewCell explained in that question by using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProductCell *cell = (ProductCell *) [tableView cellForRowAtIndexPath:indexPath];
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[self.selectedIndexes setObject:selectedIndex forKey:indexPath];
if (isSelected) {
cell.addButton.hidden = NO;
}else {
cell.addButton.hidden = YES;
}
// This is where magic happens...
[self.theMenuListTableView beginUpdates];
[self.theMenuListTableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return kCellHeight * 2.0;
}
// Cell isn't selected so return single height
return kCellHeight;
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
NSNumber *selectedIndex = [self.selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
这可以正常工作,但是我想要的是,当单击一个单元格并且对高度进行动画处理后,再单击另一个单元格时,我希望第一个单元格收缩/收缩到其原始大小,并且其他展开(动画高度变化).我怎样才能做到这一点?
This is working fine, but want I want is, that when one cell is clicked and the height has been animated, and I then click on another cell, I want the first cell to contract/collapse to it's original size and the other to expand (animate the height change). How can I do this?
推荐答案
也许有帮助:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
cell.selectionStyle = UITableViewCellEditingStyleNone;
return cell;
}
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
if (indexPath.row > 0) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section];
[tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationNone];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return 40 * 2.0;
}
// Cell isn't selected so return single height
return 40;
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
return (tblView.indexPathForSelectedRow.row == indexPath.row);
}
这篇关于单击另一个单元格时,将UITableViewCell折叠为原始大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文