我一直在尝试实现一个视图,其中有一个表,单击时应该展开列。
我确实改变了heightForRowAtIndexPath
方法以返回我想要的高度。我确实放了调试指针并检查了返回的内容,我的逻辑是否正常。在这种情况下,它返回44(我想要的)和100,如果我点击一个单元格。那很好。但在模拟器中,所有单元格都是100高度,但heightForRowAtIndexPath
从未为任何单元格返回该值。
我已经花了几天但没有结果。
这里有什么问题?
如何解决? 我的视图控制器代码
#import "TableViewControllerTest.h"
#import "ExpandingCell.h"
@interface TableViewControllerTest ()
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation TableViewControllerTest
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.delegate = self;
self.tableView.dataSource = self;
selectedIndex = -1;
titleArray = [[NSMutableArray alloc] init];
NSString *string;
for(int i=1 ; i<=8; i++){
string = [[NSString alloc] initWithFormat:@"Row %i",i];
[titleArray addObject:string];
}
subtitleArray = [[NSArray alloc] initWithObjects:@"First Row", @"Second Row",@"Third Row",@"Fourth Row",@"Fifth Row",@"Sixth Row",@"Seventh Row",@"Eighth Row",nil];
textArray = [[NSArray alloc] initWithObjects:@"Apple",@"Orange",@"Banana",@"Grape",@"blueberry",@"Lemon",@"Lime",@"Peach", nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"expandingCell";
ExpandingCell *cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExpandingCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(selectedIndex == indexPath.row){
cell.contentView.backgroundColor = [UIColor lightGrayColor];
cell.textLable.textColor = [UIColor whiteColor];
cell.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
cell.titleLabel.textColor = [UIColor whiteColor];
cell.subtitleLabel.textColor = [UIColor whiteColor];
cell.calcLabel.textColor = [UIColor whiteColor];
cell.calculationLabel.textColor = [UIColor whiteColor];
cell.fruitLabel.textColor = [UIColor whiteColor];
}
else{
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLable.textColor = [UIColor blackColor];
cell.titleLabel.font = [UIFont systemFontOfSize:17];
cell.titleLabel.textColor = [UIColor blackColor];
cell.subtitleLabel.textColor = [UIColor blackColor];
cell.calcLabel.textColor = [UIColor blackColor];
cell.calculationLabel.textColor = [UIColor blackColor];
cell.fruitLabel.textColor = [UIColor blackColor];
}
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.subtitleLabel.text = [subtitleArray objectAtIndex:indexPath.row];
cell.textLable.text = [textArray objectAtIndex:indexPath.row];
int calculation = (int)(indexPath.row +1) * 25;
cell.calculationLabel.text = [NSString stringWithFormat:@"%i",calculation];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (selectedIndex == indexPath.row) {
return 100;
}
else{
return 44;
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if(selectedIndex == indexPath.row){
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if(selectedIndex != -1){
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}