Mar*_*ews 3 iphone height expand cell show-hide
我需要在选择时扩展/收缩单元格,我在这里使用了很棒的教程.但是,我还需要在选择时显示/隐藏UILabel - 就像详细视图一样,当您展开单元格时,会显示更多标签; 将其恢复到原始大小,并再次隐藏这些标签.
基本上:
单击"
延长单元格长度"
显示标签
再次单击
合同单元
隐藏标签
一次只打开一个单元格
这听起来很容易,但是在网络上普遍使用的方法(我上面链接的教程)会在第一次触摸时自动取消选择其单元格,这意味着我隐藏的UILabel永远不会有机会出现.
如果我删除
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
Run Code Online (Sandbox Code Playgroud)
从didSelectRowAtIndexPath,我可以得到隐藏的标签,但它当然不会消失,因为在我选择不同的单元格之前,单元格不会被取消选择.
如何在单元用户第二次单击它后返回到常规高度时,单元格是否会自动取消选择?此外,有没有办法将表限制为一次只有一个扩展单元格,因为现在,您可以完全展开所有单元格.如果扩展Cell2会自动缩小Cell1回到原来的高度,我会喜欢它.
谢谢大家; 如果没有Stack Overflow,我不知道我会做什么.
TableViewController.h
@interface TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *tableView;
NSMutableDictionary *selectedIndexes;
}
@end
Run Code Online (Sandbox Code Playgroud)
TableViewController.m中的相关代码
@interface TableViewController (private)
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
@end
@implementation TableViewController
#define kCellHeight 50.0
- (void)viewDidLoad {
[super viewDidLoad];
selectedIndexes = [[NSMutableDictionary alloc] init];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
- (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];
}
return cell;
}
#pragma mark -
#pragma mark Tableview Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
// This is where magic happens...
[tableView beginUpdates];
[tableView 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;
}
@end
Run Code Online (Sandbox Code Playgroud)
另外,来自UITableCell类的UILabels:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected == YES){
date.hidden = NO;
}
else if (selected == NO) {
date.hidden = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
我是个白痴.最简单的解决方案始终是最好的,这一行是一行代码:
self.contentView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
当你设置你的细胞并将其铺设时.
咄.