I am developing an iOS application. I have used a table view. When the user clicks to any cell then the cell expands. But some cells are not expanding in iOS 7. Other versio
It would be better if you subclass and UITableViewCell and use the layoutSubviews to adjust when you adjust the size of the cell.
//In SMTableViewCell.h
@interface SMTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *seeMoreButton;
//SMTableViewCell.m
- (void)layoutSubviews
{
CGRect labelFrame = self.statusLabel.frame;
labelFrame.size.height = self.frame.size.height - 55.0f;
self.statusLabel.frame = labelFrame;
CGRect buttonFrame = self.seeMoreButton.frame;
buttonFrame.origin.y = labelFrame.origin.y+labelFrame.size.height+10.0f;
self.seeMoreButton.frame = buttonFrame;
}
Keep an array to store the selectedIndexPaths
@property (nonatomic, strong) NSMutableArray *selectedIndexPaths;
Calculate the height of the cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
CGFloat maxHeight = MAXFLOAT;
CGFloat minHeight = 40.0f;
CGFloat constrainHeight = isSelected?maxHeight:minHeight;
CGFloat constrainWidth = tableView.frame.size.width - 20.0f;
NSString *text = self.items[indexPath.row];
CGSize constrainSize = CGSizeMake(constrainWidth, constrainHeight);
CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:15.0f]
constrainedToSize:constrainSize
lineBreakMode:NSLineBreakByCharWrapping];
return MAX(labelSize.height+75, 100.0f);
}
Initialize custom Show more TableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
SMTableViewCell *cell= (SMTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SMTableViewCell class])
owner:nil
options:nil] lastObject];
}
BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
cell.statusLabel.numberOfLines = isSelected?0:2;
NSString *text = self.items[indexPath.row];
cell.statusLabel.text = text;
NSString *buttonTitle = isSelected?@"See Less":@"See More";
[cell.seeMoreButton setTitle:buttonTitle forState:UIControlStateNormal];
[cell.seeMoreButton addTarget:self action:@selector(seeMoreButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.seeMoreButton setTag:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Button click event method
- (void)seeMoreButtonPressed:(UIButton *)button
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
[self addOrRemoveSelectedIndexPath:indexPath];
}
- (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath
{
if (!self.selectedIndexPaths) {
self.selectedIndexPaths = [NSMutableArray new];
}
BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath];
if (containsIndexPath) {
[self.selectedIndexPaths removeObject:indexPath];
}else{
[self.selectedIndexPaths addObject:indexPath];
}
[self.tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
Same Event is given if the cell is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self addOrRemoveSelectedIndexPath:indexPath];
}
Sample Demo project link.