[英]Expanding and Collapsing a tableView cell upto label's text
我在TableViewCell中有一个标签,其中有多行文本。 最初在标签上只显示一行。 我在那个单元格上有一个按钮。 我想通过单击按钮直到标签文本的最高位置来扩展单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"tabCell";
_cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *device = [self.persons objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel*)[_cell.contentView viewWithTag:2];
nameLabel.text = [NSString stringWithFormat:@"%@", [device valueForKey:@"name"]];
UILabel *dateLabel = (UILabel *)[_cell.contentView viewWithTag:3];
dateLabel.text = [NSString stringWithFormat:@"%@",[device valueForKey:@"date"]];
UILabel *descLabel = (UILabel *)[_cell.contentView viewWithTag:4];
//descTextView.text = [NSString stringWithFormat:@"%@",[device valueForKey:@"desc"]];
UIImageView *personImage = (UIImageView *)[_cell.contentView viewWithTag:1];
UIImage *personImg = [UIImage imageWithData:[device valueForKey:@"image"]];
personImage.image = personImg;
UIButton *viewMoreButton = (UIButton *)[_cell.contentView viewWithTag:5];
[viewMoreButton addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[device valueForKey:@"desc"]
attributes:@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:17]}];
reqFrame=[attrString boundingRectWithSize:CGSizeMake(descLabel.frame.size.height, CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
descLabel.attributedText = attrString;
return _cell;
}
- (void)myAction{
//what to write here?
}
首先,在cellForRowAtIndexPath
构建单元不是一个好习惯。 请改用willDisplayCell
。 看看这里为什么 。
其次,要执行所需的操作,必须在heightForRowAtIndexPath
设置所需的高度。 完成此操作后,您可以在按钮选择器调用中使用刷新特定单元格
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
实现是类似的:
- (void)buttonSelector
{
myLabel.text = @"YOUR TEXT";
[myLabel sizeToFit];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
...
return yourLabel.height;
}
无论如何,都需要获取myAction()
在其上点击按钮的单元格和indexPath的 引用 。
创建一个私有变量CGFloat newCellHeight;
并将其初始化为0.0。 让对关联单元格的引用为selectedCell
,所选索引路径为indexPathOfTappedCell
。
- (void)myAction{
UILabel *descLabel = (UILabel*)[selectedCell.contentView viewWithTag:4];
[descLabel sizeToFit]; // Automatically increases the height of the label as required
newCellHeight = CGRectGetMaxY(descLabel.frame) + 10.0; // Extra padding 10.0
[tableView reloadRowsAtIndexPaths:@[indexPathOfTappedCell] withRowAnimation:UITableViewRowAnimationAutomatic];
}
现在在您的视图控制器中添加以下TableView委托方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == indexPathOfTappedCell.section &&
indexPath.row == indexPathOfTappedCell.row ) {
return newCellHeight;
}
return 44.0; // Suppose default height is 44.0
}
您需要维护两个变量,只需给按钮添加标签,然后通过其标签进行操作以重新加载该特定单元格,然后使用另一个BOOL变量来检测按钮是否被触摸。 您应该在heightForRowAtIndexPath方法中计算高度。 我正在发布代码,可能会对您有所帮助
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(isReadMoreButtonTouched && [indexPath row]== indexOfReadMoreButton) {
NSString *yourText = [arr1 objectAtIndex:indexPath.row]; // your text
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Avenir Next" size:14], NSFontAttributeName,
[UIColor grayColor], NSForegroundColorAttributeName,
nil]; // set custom attributes
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:yourText attributes:attributes];
CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil]; //here 625 is width of label
NSLog(@"height = %f", paragraphRect.size.height);
return paragraphRect.size.height;
}
else{
return 200; //default height taken in storyboard
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.lblKB.text = [arr objectAtIndex:indexPath.row];
cell.lblDetail.text = [arr1 objectAtIndex:indexPath.row];
NSString *yourText = [arr1 objectAtIndex:indexPath.row];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Avenir Next" size:14], NSFontAttributeName,
GrayColor, NSForegroundColorAttributeName,
nil]; //your custom attributes
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:yourText attributes:attributes];
CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
NSLog(@"height = %f", paragraphRect.size.height);
if (paragraphRect.size.height<200) {
cell.btnMore.hidden = YES;
}
else{
cell.btnMore.hidden = NO;
}
cell.btnMore.tag = indexPath.row;
return cell;
}
// action of button click
-(IBAction)MoreBtnClicked:(id)sender {
if (!isReadMoreButtonTouched) {
isReadMoreButtonTouched = YES;
}
else{
isReadMoreButtonTouched = NO;
}
indexOfReadMoreButton = [sender tag];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
[self.tblKB reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; //reload that cell of your tableview
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.