点击cell显示cell的细节
来源:互联网 发布:网店运维 s5.1源码 编辑:程序博客网 时间:2024/05/15 04:26
实现的功能:点击cell显示cell的详细信息,再次点击关闭cell的详细信息。
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
{
UILabel *lab_info;
UILabel *lab_detailInfo;
CGFloat normalHeight;
}
@property (retain,nonatomic)UILabel *lab_info,*lab_detailInfo;
@property CGFloat normalHeight;
-(CGFloat)getMyCellHeight;
-(void)resetCellHeight;
@end
#import "MyCell.h"
@interface MyCell()
{
}
@end
@implementation MyCell
@synthesize lab_info,lab_detailInfo;
@synthesize normalHeight;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
lab_info=[[UILabel alloc]init];
[self.contentView addSubview:lab_info];
//
self.normalHeight=self.frame.size.height;
//
lab_detailInfo=[[UILabel alloc]init];
lab_detailInfo.lineBreakMode=NSLineBreakByCharWrapping;
lab_detailInfo.numberOfLines=5;
[self.contentView addSubview:lab_detailInfo];
//
self.layer.shadowColor=[UIColor blackColor].CGColor;
self.layer.shadowOffset=CGSizeMake(0, 2);
self.layer.shadowOpacity=0.3;
self.layer.shadowRadius=2;
//
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
-(CGFloat)getDetailLabViewHeight
{
[self.lab_detailInfo sizeToFit];
return self.lab_detailInfo.frame.size.height;;
}
-(CGFloat)getMyCellHeight
{
return normalHeight +[self getDetailLabViewHeight];
}
-(void)resetCellHeight
{
[self layoutIfNeeded];
CGRect cellFrame=self.frame;
cellFrame.size.height=normalHeight+[self getDetailLabViewHeight];
[self setFrame:cellFrame];
}
-(void)layoutSubviews
{
[super layoutSubviews];
//设置标题的lab的高度
CGRect lab_info_Frame=CGRectInset(self.frame, 0, 0);
lab_info_Frame.origin.x=lab_info_Frame.origin.y=0;
lab_info_Frame.size.height=normalHeight;
[lab_info setFrame:lab_info_Frame];
//设置详细信息的高度
CGRect lab_detailInfo_Frame=CGRectInset(self.frame, 0, 0);
lab_detailInfo_Frame.origin.x=0;
lab_detailInfo_Frame.origin.y=normalHeight;
lab_detailInfo_Frame.size.height=[self getDetailLabViewHeight];
[lab_detailInfo setFrame:lab_detailInfo_Frame];
//
//[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
self.contentView.backgroundColor=[UIColor grayColor];
lab_info.backgroundColor=[UIColor clearColor];
lab_detailInfo.backgroundColor=[UIColor clearColor];
}
@end
#import <UIKit/UIKit.h>
#import "MyCell.h"
@interface RootViewController : UITableViewController
{
NSMutableArray *isItemSelected;
}
@end
#import "RootViewController.h"
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
isItemSelected=[[NSMutableArray alloc]initWithCapacity:5];
for (int i=0; i<5; i++)
{
[isItemSelected insertObject:[NSNumber numberWithBool:NO] atIndex:i];
}
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (MyCell*)[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
cell.lab_info.text=[NSString stringWithFormat:@"title-%d",indexPath.row];
cell.lab_detailInfo.text=[NSString stringWithFormat:@"subtitle-%d",indexPath.row];
if ([[isItemSelected objectAtIndex:indexPath.row]boolValue]==YES) {
cell.lab_detailInfo.hidden=NO;
}else{
cell.lab_detailInfo.hidden=YES;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell=(MyCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
if ([[isItemSelected objectAtIndex:indexPath.row] boolValue]==YES) {
return [cell getMyCellHeight];
}
else
{
return cell.normalHeight;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL b=[[isItemSelected objectAtIndex:indexPath.row]boolValue];
[isItemSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:!b]];
//
[tableView reloadData];
// [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
效果是这样的
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的详细信息,再次…
- 修改点击cell时显示的颜色
- 修改点击cell时显示的颜色
- 修改点击cell时显示的颜色
- 修改点击cell时显示的颜色
- 点击cell上面的控件获取cell
- 点击cell,在cell下方创建一个新的cell,再次点击,删除该cell
- 让cell 在点击的时候,子视图不高亮显示
- iOS点击TableView的cell显示弹出动画
- iOS点击TableView的cell显示弹出动画
- iOS点击TableView的cell显示弹出动画
- 开发细节--点击cell的时候,默认的点击效果覆盖控件背景色的问题
- tableView的cell不可点击
- MFC非模态对话框实例
- 使用httpclient抓取时,netstat 发现很多time_wait连接。解释如下
- 安装GCC(sudo yum -y install gcc 如果yum没有 sudo apt-get install yum)
- android系统中的多线程(二): 关于在work thread中对UI进行更新和设置
- JDK1.5之后ExecutorService线程池抛RejectedExecutionException的原因剖析以及解决方案
- 点击cell显示cell的细节
- 浏览器返回结果解压
- 如何判断操作系统是64位还是32位
- java实现简单的单点登录
- android系统中的多线程(三):关于定时和周期性执行 .
- ASP.NET MVC3学习--视图和模型(2)
- 字符设备(三)之globalmem实例集
- struts异常不捕获也可以在控制台和日志文件输出
- maven管理项目如何查找jar依赖