UITableView

1、UITableview可重用特性
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>] 
if (cell == nil) {
<#statements#>
}
dequeueReusableCellWithIdentifier,从字面上理解是“出列的可重用的cell”,其实简单说就是一个cell池,里面放的就是你之前创建过的cell。使用时要注意:
1。重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)。
2。这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂。
3。设计的关键是实现cell和数据的完全分离 
这个问题答案核心是这个机制要解决什么样的问题。
关键点在"一个屏幕显示的cell数量"是有限的
当屏幕滚动时候,就会调用方法获取新的cell,而老的cell会在屏幕外面就不显示了
reuse机制就是这样。。当cell需要显示的时候,从queue里面找,找到了,设置一下内容,显示出来
滚动界面当有cell被移出屏幕时,把这个cell丢到queue里面
显示新的cell时,如果有“相同类型”(identifier)的cell,就从队列拿一个出来,设置数据,显示出来
至于queue里面会有多少cell,这个会自动控制
要注意的是,queue里面存储的是cell的实例,不是“原型”
因此就会出现上面说的“假设每页有 5个。则第6个复用第1个cell;第7个复用第2个;”
这样的结果是不管你的table有多少行,内存里实际上都只需要存储一个屏幕那么多行的cell就搞定了。。


一些属性

 

        _mytableview.clipsToBounds = NO;//tableview中的子视图如果超过边界是否显示 NO是不显示


        _mytableview.scrollEnabled = NO;//禁止滚动


2、UITableView属性设置 ( .h要设置UIViewController<UITableViewDataSource,UITableViewDelegate>)
//UITableViewStyleGrouped 风格(组团风格)
_searchTableView = [[UITableViewalloc]initWithFrame:CGRectMake(28+60, 75+30, 294, 620) style:UITableViewStyleGrouped];
_searchTableView.separatorColor = [UIColorclearColor];
_searchTableView.backgroundColor = [UIColorclearColor];
_searchTableView.backgroundView.backgroundColor = [UIColorclearColor];
_searchTableView.backgroundView = nil;
_searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_searchTableView.showsHorizontalScrollIndicator = NO;
_searchTableView.showsVerticalScrollIndicator = NO;
_searchTableView.delegate = self;
_searchTableView.dataSource = self;
[_entrySearchView addSubview:_searchTableView];
3、去掉tableview的header
#pragma mark 去掉tableview的header设置1
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1;
}
#pragma mark 去掉tableview的header设置2
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
view.backgroundColor = [UIColorclearColor];
return [view autorelease];
}


4、横屏和竖屏转换的时候tableview设置 (注:**还要在转屏的函数里写 UITableView的reloadData函数**- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
if (UIInterfaceOrientationIsPortrait(deviceDirection))
{
//竖屏
CellIdentifier = @"CellPortrait";
}
else
{
//横屏
CellIdentifier = @"CellLandscape";
}

。。。。。。。

 

5、cell的背景一点就消失

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

   

//取消背景点中效果

cell.selectionStyle = UITableViewCellSelectionStyleNone;



}
建立 UITableView

-、建立 UITableView
DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
[DataTable release];


二、UITableView各Method说明


//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return TitleData;
}


// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"";
}


//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
return 4;
}

//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}


//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) 
{ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}

//cell设置为不可选
cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=//.....
return cell;
}

 

//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
return row;
}

 

//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}


//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];


//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];

 

//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失    
}

 

//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
{
return nil;
}
return indexPath;
}

 

//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{

}

 

//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{

}

[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];

//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
}

//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
}

//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath


//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];


三、在UITableViewCell上建立UILable多行显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
if (cell == nil) 
{    
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];    
UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];    
[Datalabel setTag:100];    
Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    
[cell.contentView addSubview:Datalabel];    
[Datalabel release];
} 
UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];    
[Datalabel setFont:[UIFont boldSystemFontOfSize:18]];    
Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}


//选中cell时的颜色
typedef enum 
{
UITableViewCellSelectionStyleNone,    
UITableViewCellSelectionStyleBlue,    
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle

//cell右边按钮格式
typedef enum 
{
UITableViewCellAccessoryNone, // don't show any accessory view    
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track    
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks    
UITableViewCellAccessoryCheckmark // checkmark. doesn't track
} UITableViewCellAccessoryType

//是否加换行线
typedef enum 
{
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
}UITableViewCellSeparatorStyle//改变换行线颜色

tableView.separatorColor = [UIColor blueColor];

        //关闭UITableView cell的被选中效果

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

 //鼠标(手指抬起的时候)背景会慢慢恢复(渐变的)

[tableView deselectRowAtIndexPath:indexPath animated:YES];

 

//鼠标(手指抬起的时候)背景就是直接恢复了

[tableView deselectRowAtIndexPath:indexPath animated:NO];

 

     //插入数据

        NSMutableArray *indexPaths = [NSMutableArray array];

        NSInteger currentCount = self.cashCardMutaArr.count;

        for (int i = 0; i < self.cashCardMutaArr.count; i++)

        {

            [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];

        }

        

        //修改tableview  插入新的cell

        [self.mytableview beginUpdates];

        // first update the data modal

        [self.mytableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

        [self.mytableview endUpdates];

 

 

 

posted @ 2012-12-13 10:00  六界剑仙  阅读(232)  评论(0)    收藏  举报