UITableView和UICellTableView
UITableView和UICellTableView
继承自UISourceView
表中的每行都代表一个UITableViewCell
cell的内容包括:图像 文本 辅助图标
contentView backgroundView selectedBackgroundView accessoryView
contentVIew 包含一个imageView和一个textLable
当cell的风格是subtitle的时候还会多出一个detailTextLable
//必须实现
实现协议UITableViewDelegeta和UITableViewDataSource
设置两个代理 setDelegate setDataSource
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section;
这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。
表格属性
.style
//索引风格
UITableViewStylePlain
//分组风格
UITableViewStyleGrouped
//行高
.rowHeight
//区域块头部高
.sectionHeaderHeight
//区域块底部高
.sectionFooterHeight
//背景视图(UIView *)
.backgroundView
(UITableViewCell)
//文字的设置
textLabel/detailTextLabel
//图片
imageView
//选中状态
BOOL selected
UITableViewCellSelectionStyle selectionStyle
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
//辅助按钮
UIView *accessoryView
UITableViewCellAccessoryType accessoryType
//添加文字图片
contentView
//可以修改背景颜色和添加背景图片
backgroundView
//可以修改点击cell时的背景颜色和背景图片
可以给当前单元(UITableViewCell)增加子View
(NSIndexPath)
row://当前行数
section://当前的区段
//根据当前行数和区段返回NSIndexPath
+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
委托方法
使用委托方法是为了相应用户的交互动作,比如下拉更新数据和选择某一行单元格
,在UITableView中有很多这种方法供我们选择
//设置Section的 数量
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
//设置每个section显示的Title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSIneger)section
//设置有多少个区域块 默认为1
- (NSIndeger)numberOfSectionsInTableView:(UITableView *)tableView
//指定每区域块中有多少行 默认为1
- (NSIndeger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
//设置每行调用的cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
1.设置static NSString变量
2.建立对象UITableView 并设置重用 * cell
3.if(cell == nil)
{
style //单元格风格
reuseIdentifier //设置重用标示符
初始cell initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
}
4.设置cell
5.返回cell
}
//设置让UITableView行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITable *)tableView heightForRowAtIndexPath
:(NSIndexPath *)indexPath
//返回当前所选的cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSecion:section];
[TapicsTable selectRowAtIndexPath:ip animated YES scrollPostion:UITableVIewScrollPositionNone];
//设置选中Cell后的响应事件
- (void)tableView:(UITableVIew *)tableView didSelectRowAtIndexPath
(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选择后的反颜色立刻消失
}
//设置选中的行执行的动作
- (NSIndexPath *)tableView :(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUIInterger row = [indexPath row];
return indexPath;
}
//设置滑动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//设置删除时的状态
- (void)tableView:(UITable *)tableView commitEditingStyle
:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableVIew
//移动tableViewCell
- tableView: moveRowAtIndexPath:toIndexPath:
//删除tableViewCell
- tableView:commitEditingStye:forRowAtIndexPath:
- //选中cell时的颜色,在官方文档有如下可以选择
- typedef enum {
- UITableViewCellSelectionStyleNone,
- UITableViewCellSelectionStyleBlue,
- UITableViewCellSelectionStyleGray
- } UITableViewCellSelectionStyle
- //cell右边按钮格式
- typedef enum {
- UITableViewCellAccessoryDetailDisclosureButton, UITableViewCellAccessoryCheckmark
- } UITableViewCellAccessoryType
- //是否加换行线
- typedef enum {
- UITableViewCellSeparatorStyleNone,
- UITableViewCellSeparatorStyleSingleLine
- } UITableViewCellSeparatorStyle
- //改变换行线颜色
- tableView.separatorColor= [UIColor blueColor];