因为有的TableViewCell文本过多导致界面太长了不方便查看,一般过长的文本先显示一部分,等点击“查看更多”时才显示全部。
代码是把cell中的view和BOOL类型的showPartner设成全局变量,然后把点击“查看更多”后才显示的内容放在判断里。
- (void)initChildView2
{
view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 40, 320, 270)];
view2.backgroundColor = [UIColor clearColor];
[backgroundView addSubview:view2];
UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 15)];
label1.text = @"最佳搭档";
label1.textColor = kColorWithRGB(38, 79, 145);
label1.font = [UIFont systemFontOfSize:15];
[view2 addSubview:label1];
UIImageView * imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 35, 45, 45)];
[imgView1 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg",[self.herodetail.like[0] objectForKey:@"partner"]]] placeholderImage:nil];
[view2 addSubview:imgView1];
CGSize size = CGSizeMake(225, 200);
CGSize label2Size = [[self.herodetail.like[0] objectForKey:@"des"] sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(75, 35, 225, label2Size.height)];
label2.text = [self.herodetail.like[0] objectForKey:@"des"];
[label2 setNumberOfLines:0];
label2.font = [UIFont systemFontOfSize:15];
[view2 addSubview:label2];
CGSize label3Size = CGSizeZero;
if (self.showPartner) {
UIImageView * imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50 + label2Size.height, 45, 45)];
[imgView2 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg",[self.herodetail.like[1] objectForKey:@"partner"]]] placeholderImage:nil];
[view2 addSubview:imgView2];
label3Size = [[self.herodetail.like[1] objectForKey:@"des"] sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(75, 50 + label2Size.height, 225, label3Size.height)];
label3.text = [self.herodetail.like[1] objectForKey:@"des"];
[label3 setNumberOfLines:0];
label3.font = [UIFont systemFontOfSize:15];
[view2 addSubview:label3];
}
UIButton * hiddenBtn = [[UIButton alloc]initWithFrame:CGRectMake(130, 50 + label2Size.height + label3Size.height, 60, 15)];
[hiddenBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
hiddenBtn.tag = 1006;
hiddenBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[hiddenBtn addTarget:self action:@selector(hiddenDetailView:) forControlEvents:UIControlEventTouchUpInside];
if (self.showPartner) {
[hiddenBtn setTitle:@"隐藏" forState:UIControlStateNormal];
}
else
{
[hiddenBtn setTitle:@"查看更多" forState:UIControlStateNormal];
}
[view2 addSubview:hiddenBtn];
view2.frame =CGRectMake(0, 0, 320, 60+ label2Size.height + label3Size.height+hiddenBtn.frame.size.height);
}
因为有好几处用到【查看全部】与【隐藏】模式,于是点击按钮时写了个公用的方法体,通过tag值区分
#pragma mark 【显示全部】与【隐藏】
- (void)hiddenDetailView:(UIButton *)sender
{
NSIndexPath * index;
switch(sender.tag)
{
case 1006:
{
self.showPartner = ! self.showPartner;
index = [NSIndexPath indexPathForRow:1 inSection:0];
}
break;
case 1007:
{
self.showHate = ! self.showHate;
index = [NSIndexPath indexPathForRow:2 inSection:0];
}
break;
case 1008:
{self.showDescription = ! self.showDescription;
index = [NSIndexPath indexPathForRow:6 inSection:0];
}
break;
default:
break;
}
[_infomationTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
}
因为每栏都是TableViewCell,所以点击按钮【查看更多】或者【隐藏】后,只需重载TableView中的对应行即可