我正在尝试创建一个 tableView,如果您点击一个单元格,它会展开并显示到按钮。到目前为止我遇到的问题是,如果我创建带有按钮的单元格,然后说它应该只有完整的高度,如果您按下它,它仍然显示按钮,但它们与 tableView 中的下一个单元格重叠
点击之前应该隐藏的单元格部分是否确实可以做到这一点?
这就是我在 viewController.m 中的内容 您在下面找到的 ExpandingCell.h
#import "ViewController.h"
#import "ExpandingCell.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (copy, nonatomic) NSArray *titleArray;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
ExpandingCell *cell;
int selectedIndex = -1;
- (void)viewDidLoad {
[super viewDidLoad];
self.titleArray = @[@"Apple", @"Orange", @"Banana", @"Kiwi", @"Melon", @"Strawberry", @"Blueberry", @"Peach"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.titleArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"expandingCell";
cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.titleLabel.text = self.titleArray[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 94;
} else {
return 54;
}
}
@end
ExpandCell.h 只是单元格上的一些标签和按钮
#import <UIKit/UIKit.h>
@interface ExpandingCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutlet UILabel *backgroundLabel;
@end
仅出于文本目的,我设置了第一个单元格,就像在“tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath”方法中选择它一样。
我得到的是一个表格 View ,其中第一个单元格完美显示其高度,然后其余单元格的按钮与下面的下一个单元格重叠。
(我想显示屏幕截图,但它说我需要 10 个声誉才能这样做)
如果是这样,我真的很感谢您的帮助
最佳答案
您应该实现一个 tableView:heightForRowAtIndexPath:
并返回取决于单元格状态(展开与否)的行高度。然后点击您的单元格后调用 [self.tableView reloadRowsAtIndexPaths:@[indexPathForYourCell]]
。为了防止重叠,请将单元格的 clipsToBounds
属性设置为 YES。
关于ios - 扩展 UITableViewCell 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29718539/