最近有一个需求 就是对于tableviewCell的显示与隐藏的处理.就是点击cell有部分文字展示出来,右侧的箭头向下,再点击文字隐藏,右侧箭头向上,大致如下图:
实现的大致思路是这样的
第一步 : 在控制器里
1.在数据的数组里添加一组键值对 @"isChoice":@"NO" ,
2.当点击某一个cell时 会走tableview代理方法的 didSelectRowAtIndexPath,在这里面 进行判断
if([dic[@"isChoice"]isEqualToString:@"NO"]) {
[dictempsetObject:@"YES"forKey:@"isChoice"];
[self.dataArrayreplaceObjectAtIndex:indexPath.rowwithObject:dictemp];
}else{
[dictempsetObject:@"NO"forKey:@"isChoice"];
[self.dataArrayreplaceObjectAtIndex:indexPath.rowwithObject:dictemp];
}
然后进行刷新 [tableView reloadData];
3.在tableview的数据源方法 cellForRowAtIndexPath 里 将数据赋值给cell
cell.dicinfo=self.dataArray[indexPath.row];
第二步:
在cell 里的
@property(nonatomic,strong)NSDictionary*dicinfo;
用dicinfo来接收数据,在 set方法里-(void)setDicinfo:(NSDictionary*)dicinfo{}
进行判断 当 @"isChoice":@"NO"时 Label不赋值 UIimage 箭头向下的图片
当@"isChoice":@"YES"时 Label赋值 UIimage 箭头向上的图片
-(void)setDicinfo:(NSDictionary*)dicinfo{
_dicinfo= dicinfo;
if([_dicinfo[@"isChoice"]isEqualToString:@"NO"]) {
self.storeLabel.text=@"";
self.jiantouimgView.image= [UIImageimageNamed:@"箭头3.2.2.2"];
}else{
self.storeLabel.text= [NSStringstringWithFormat:@"A: %@",dicinfo[@"answer"]];
self.jiantouimgView.image= [UIImageimageNamed:@"下箭头3.2.2.2"];
NSMutableAttributedString*attributeStr1 = [[NSMutableAttributedStringalloc]initWithString: [NSStringstringWithFormat:@"A: %@",dicinfo[@"answer"]]];
[attributeStr1addAttribute:NSForegroundColorAttributeNamevalue:COLOR_MAINrange:NSMakeRange(0,2)];
self.storeLabel.attributedText= attributeStr1;
}
}
注: 我是用Masonry的,在布局cell的时候由于数据文字多少每个cell不一致所以要自适应高度所以在布局cell的时候要注意要让cell内的控件撑起来cell最上面的控件top根据self的top布局最下面的控件bottom根据self的bottom布局中间的上下连接 .
如果我的思路对你有帮助 ,是我的荣幸.谢谢 .
网友评论