一)UITableView所在的UIViewController声明两个delegate:UITableViewDelegate和UITableViewDataSource。
二)将UITableView对象的delegate设置成self。
三)根据实际需要实现delegate的具体方法,这里简要介绍一下常用的方法和属性。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 该方法返回tableview有多少个section。
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 该方法返回对应的section有多少个元素,也就是每个section对应有多少个cell。
(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定的row高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 该方法返回指定的section的header view的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 该方法返回指定的section的footer view的高度。
(4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定row的cell,在此函数中用户可以根据自己的需求定义cell的属性和显示风格等(主标题cell.textLabel,副标题cell.detailTextLabel,背景cell.imageView,图标cell.accessoryType等等)。
(5)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 该函数返回指定section的header的titile。
(6)- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 该函数返回指定section header的view
(7) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 用户选中某cell时的回调函数。
//<============我是淫荡的分割线(二)====================>
(8)- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法获取某一cell对象。
(9)如果想让cell能响应选中事件,但是选中后的颜色不发生改变的话,设置cell.selectionStyle = UITableViewCellSelectionStyleNone。
(10)如果想删除cell之间的分割线,设置
tableview.separatorStyle = UITableViewCellSeparatorStyleNone。
(11)//Section总数-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
returnTitleData;
}
(12)//行缩进-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row =[indexPath row];
returnrow;
}
(13)//定位 scrollview的方法[TopicsTable setContentOffset:CGPointMake(0, promiseNum *44+Chapter *20)];
(14)//返回当前所选cell 即是(8)
NSIndexPath *ip =[NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
(15)//判断选中的行(阻止选中第一行)-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row =[indexPath row];
if(row ==0)
return nil;
return indexPath;
}
(16)-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath//划动cell是否出现del按钮
(17)-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath//编辑状态
(18)-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView//右侧添加一个索引表
(19)-(NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath//自定义划动时del按钮内容
(20)//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0]
(21)atScrollPosition:UITableViewScrollPositionBottom animated:NO];
(22)tableView:accessoryButtonTappedForRowWithIndexPath 响应用户点击cell右边的箭头事件
//<============我是淫荡的分割线(三)====================>
三、在UITableViewCell上建立UILable多行显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *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;
returncell;
}
//选中cell时的颜色typedef enum{
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式typedef enum{
UITableViewCellAccessoryNone, //don't show any accessory viewUITableViewCellAccessoryDisclosureIndicator, //regular chevron. doesn't trackUITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracksUITableViewCellAccessoryCheckmark //checkmark. doesn't track} UITableViewCellAccessoryType
//是否加换行线typedef enum{
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle//改变换行线颜色tableView.separatorColor =[UIColor blueColor];
滚筒原理理解:
每一个UITableView里都维护着一个cell队列,当UITableView刚加载的时候,cell队列里是没有任何数据的。所以就有if(cell==nil)
dequeueResableCellWithIdentifier从字面上理解就是”出列可重用的cell",也就是根据一个标识identifier从cell队列里取出一个
UITableViewCell,当然了,如果cell队列里没有此标识的cell,调用此方法的结果就是返回nil。因此,在UITableView刚加载的时候,cell队列里没有可用的cell,所以必须通过语句
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
来创建对应CellIdentifier标识的UITableViewCell实例。
[ tableView:cellForRowAtIndexPath:方法主要是根据nsindex取得一个cell ]
而当UITableView在滚动的时候导致UITableViewCell滚出手机屏幕视图的时候,程序会将这一个UITalbeViewCell实例放入此UITableView所维护的cell队列中。当UITableview中有新的UITableViewCell需要展现在手机屏幕视图上时,就会调用tableView:cellForRowAtIndexPath:方法了。
因此我们可以知道以下几点:
1-重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)。
2-这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂。
3-设计的关键是实现cell和数据的完全分离
如果不想重用UITableViewCell实例,如在一个每一行都显示不同内容的UITableView实例时,我们可以用如下的方法:
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];
来重新定义标识。
这样每一行都有其对应的identifier,从cell队列里取出来只有两个结果:
1-cell队列里没有此identifier对应的UITableViewCell实例,返回nil
2-cell队列里有此identifier对应的UITableViewCell实例,而且不会有重用到其他不同行的cell的情况
//<============我是淫荡的分割线(四)====================>
四、cell.image将不同大小的图片显示为相同大小
1、- (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];
}
// Set up the cell...
int personIndex = indexPath.row;
UIImage *image= [UIImage imageWithContentsOfFile:[[myData objectAtIndex:personIndex] objectForKey:@"path"]];
//在.nib文件中也要相应的修改列表高度; 另外resize是方法实现马上会讲到
[cell setImage:[image resize:CGRectMake(0, 0, 70, 70)]];
[cell setFont:[UIFont italicSystemFontOfSize:24]];//字体设置为斜体
[cell setText:[[myData objectAtIndex:personIndex] objectForKey:@"name"]];
return cell;
}
2、最关键的部分,如何实现调整图片大小。新建UIImageResize文件
//UIImageResize.h
@interface UIImage(Resize)
//由于是对UIImage的补充,所以名字必须和UIImage不同,但是又不能是别的,否则无法调用UIImage已有的method了;由于本身就是UIImage类,也不用写给interface{}加接口否则报错,书写格式对照framework里头的UIImage.h写就行了
-(UIImage*)resize:(CGRect)rect;
@end
//UIImageResize.m
@implementation UIImage(Resize)
//根据新的尺寸重新画一个符合tableViewCell尺寸的image
-(UIImage*)resize:(CGRect)rect{
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect];
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
@end
二)将UITableView对象的delegate设置成self。
三)根据实际需要实现delegate的具体方法,这里简要介绍一下常用的方法和属性。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 该方法返回tableview有多少个section。
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 该方法返回对应的section有多少个元素,也就是每个section对应有多少个cell。
(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定的row高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 该方法返回指定的section的header view的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 该方法返回指定的section的footer view的高度。
(4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定row的cell,在此函数中用户可以根据自己的需求定义cell的属性和显示风格等(主标题cell.textLabel,副标题cell.detailTextLabel,背景cell.imageView,图标cell.accessoryType等等)。
(5)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 该函数返回指定section的header的titile。
(6)- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 该函数返回指定section header的view
(7) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 用户选中某cell时的回调函数。
//<============我是淫荡的分割线(二)====================>
(8)- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法获取某一cell对象。
(9)如果想让cell能响应选中事件,但是选中后的颜色不发生改变的话,设置cell.selectionStyle = UITableViewCellSelectionStyleNone。
(10)如果想删除cell之间的分割线,设置
tableview.separatorStyle = UITableViewCellSeparatorStyleNone。
(11)//Section总数-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
returnTitleData;
}
(12)//行缩进-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row =[indexPath row];
returnrow;
}
(13)//定位 scrollview的方法[TopicsTable setContentOffset:CGPointMake(0, promiseNum *44+Chapter *20)];
(14)//返回当前所选cell 即是(8)
NSIndexPath *ip =[NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
(15)//判断选中的行(阻止选中第一行)-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row =[indexPath row];
if(row ==0)
return nil;
return indexPath;
}
(16)-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath//划动cell是否出现del按钮
(17)-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath//编辑状态
(18)-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView//右侧添加一个索引表
(19)-(NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath//自定义划动时del按钮内容
(20)//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0]
(21)atScrollPosition:UITableViewScrollPositionBottom animated:NO];
(22)tableView:accessoryButtonTappedForRowWithIndexPath 响应用户点击cell右边的箭头事件
//<============我是淫荡的分割线(三)====================>
三、在UITableViewCell上建立UILable多行显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *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;
returncell;
}
//选中cell时的颜色typedef enum{
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
//cell右边按钮格式typedef enum{
UITableViewCellAccessoryNone, //don't show any accessory viewUITableViewCellAccessoryDisclosureIndicator, //regular chevron. doesn't trackUITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracksUITableViewCellAccessoryCheckmark //checkmark. doesn't track} UITableViewCellAccessoryType
//是否加换行线typedef enum{
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle//改变换行线颜色tableView.separatorColor =[UIColor blueColor];
滚筒原理理解:
每一个UITableView里都维护着一个cell队列,当UITableView刚加载的时候,cell队列里是没有任何数据的。所以就有if(cell==nil)
dequeueResableCellWithIdentifier从字面上理解就是”出列可重用的cell",也就是根据一个标识identifier从cell队列里取出一个
UITableViewCell,当然了,如果cell队列里没有此标识的cell,调用此方法的结果就是返回nil。因此,在UITableView刚加载的时候,cell队列里没有可用的cell,所以必须通过语句
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
来创建对应CellIdentifier标识的UITableViewCell实例。
[ tableView:cellForRowAtIndexPath:方法主要是根据nsindex取得一个cell ]
而当UITableView在滚动的时候导致UITableViewCell滚出手机屏幕视图的时候,程序会将这一个UITalbeViewCell实例放入此UITableView所维护的cell队列中。当UITableview中有新的UITableViewCell需要展现在手机屏幕视图上时,就会调用tableView:cellForRowAtIndexPath:方法了。
因此我们可以知道以下几点:
1-重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)。
2-这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂。
3-设计的关键是实现cell和数据的完全分离
如果不想重用UITableViewCell实例,如在一个每一行都显示不同内容的UITableView实例时,我们可以用如下的方法:
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];
来重新定义标识。
这样每一行都有其对应的identifier,从cell队列里取出来只有两个结果:
1-cell队列里没有此identifier对应的UITableViewCell实例,返回nil
2-cell队列里有此identifier对应的UITableViewCell实例,而且不会有重用到其他不同行的cell的情况
//<============我是淫荡的分割线(四)====================>
四、cell.image将不同大小的图片显示为相同大小
1、- (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];
}
// Set up the cell...
int personIndex = indexPath.row;
UIImage *image= [UIImage imageWithContentsOfFile:[[myData objectAtIndex:personIndex] objectForKey:@"path"]];
//在.nib文件中也要相应的修改列表高度; 另外resize是方法实现马上会讲到
[cell setImage:[image resize:CGRectMake(0, 0, 70, 70)]];
[cell setFont:[UIFont italicSystemFontOfSize:24]];//字体设置为斜体
[cell setText:[[myData objectAtIndex:personIndex] objectForKey:@"name"]];
return cell;
}
2、最关键的部分,如何实现调整图片大小。新建UIImageResize文件
//UIImageResize.h
@interface UIImage(Resize)
//由于是对UIImage的补充,所以名字必须和UIImage不同,但是又不能是别的,否则无法调用UIImage已有的method了;由于本身就是UIImage类,也不用写给interface{}加接口否则报错,书写格式对照framework里头的UIImage.h写就行了
-(UIImage*)resize:(CGRect)rect;
@end
//UIImageResize.m
@implementation UIImage(Resize)
//根据新的尺寸重新画一个符合tableViewCell尺寸的image
-(UIImage*)resize:(CGRect)rect{
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect];
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
@end
本文详细介绍了UITableView的使用方法,包括声明delegate、实现常用方法、cell的复用机制、自定义cell显示及图片大小调整等内容。

391

被折叠的 条评论
为什么被折叠?



