- UITableView
- //滚动到当前行
- [self tableViewScrollCurrentIndexPath];
- 1..h文件里添加协议
- @interface HwjViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
- 2.声明变量
- @interface HwjViewController ()
- {
- //声明UITableView视图
- UITableView *_tableView;
- //数据源数组
- NSMutableArray *_dataArray;
- }
- 3.tableView基本设置
- - (void)creatTableView{
- //创建数据源数组 来提供数据
- _dataArray = [[NSMutableArray alloc]init];
- //给数据源数组添加数据
- for (int i = 0; i<5; i++) {
- NSString *name = [NSString stringWithFormat:@"第%d行",i];
- [_dataArray addObject:name];
- }
- //创建tableView视图
- _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
- //设置代理
- _tableView.delegate = self;
- //设置数据源 提供数据的对象
- _tableView.dataSource = self;
- //对tableView进行设置
- //设置分割线颜色
- _tableView.separatorColor = [UIColor redColor];
- //设置分割线类型
- //UITableViewCellSeparatorStyleNone没有分割线
- //UITableViewCellSeparatorStyleSingleLine 单线
- //UITableViewCellSeparatorStyleSingleLineEtched 没有线
- _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
- //设置头视图 只有高度有效
- UIImageView *HeaderView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
- //头视图高度有影响
- HeaderView.image = [UIImage imageNamed:@"huancun.jpg"];
- //设置头视图
- _tableView.tableHeaderView = HeaderView;
- //设置尾视图
- UIImageView *FooterView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
- FooterView.image = [UIImage imageNamed:@"huancun.jpg"];
- //设置尾视图
- _tableView.tableFooterView = FooterView;
- [self.view addSubview:_tableView];
- }
- 4.代理基本设置
- #pragma mark - 创建cell
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- //cell复用标志
- static NSString *cellID = @"cellId";
- //从复用队列获取是否有cellID标志对应的可复用的cell
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
- if (cell == nil) {
- //创建cell
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
- //cell上有一个contentView ,cell 上的内容都粘到了contentView上 所有我们要想在cell 上粘贴自己的内容必须要在contentView 上粘贴
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(290, cell.contentView.bounds.size.height/2, 15, 15)];
- label.tag = 1001;//设置tag 值
- [cell.contentView addSubview:label];
- [label release];
- //把一个label 粘贴到contentView上
- }
- NSLog(@"contentView:%@",cell.contentView);
- UILabel *label = (UILabel *)[cell.contentView viewWithTag:1001];
- label.text = [NSString stringWithFormat:@"%d",indexPath.row];//显示哪一行
- //indexPath:保存的是第几分区的第几行
- //分区:indexPath.section 行:indexPath.row
- //对cell 内容进行修改
- cell.textLabel.text = [NSString stringWithFormat:@"第%c分区",indexPath.section+'A'];
- //获取数据源的数据
- //1.先得到分区数据 一个一维数组
- // NSMutableArray *arr = _dataArray[indexPath.section];
- // cell.detailTextLabel.text = [arr objectAtIndex:indexPath.row];
- cell.detailTextLabel.text = _dataArray[indexPath.section][indexPath.row];
- //设置cell的左侧图片
- cell.imageView.image = [UIImage imageNamed:@"11.jpeg"];
- /*
- //没有选中效果
- UITableViewCellSelectionStyleNone,
- //下面几个都是灰色风格的
- UITableViewCellSelectionStyleBlue,
- UITableViewCellSelectionStyleGray,
- UITableViewCellSelectionStyleDefault
- */
- //选中之后都是灰的
- //设置cell 的选中风格
- cell.selectionStyle = UITableViewCellSelectionStyleDefault;
- //设置选中背景视图
- UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 320, 44)];
- //坐标大小没有影响
- redView.backgroundColor = [UIColor redColor];
- cell.selectedBackgroundView = redView;
- [redView release];
- //设置背景颜色
- cell.backgroundColor = [UIColor grayColor];
- //设置背景图片
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
- //table_cell_bg比较小 我们可以自己实现拉伸
- imageView.image = [[UIImage imageNamed:@"table_cell_bg"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
- //stretchableImageWithLeftCapWidth拉伸图片
- //距离左侧5像素 不拉伸 从第6像素开始拉伸
- //距离顶部5像素 不拉伸 从第6像素开始拉伸
- //应用气泡聊天
- cell.backgroundView = imageView;
- [imageView release];
- return cell;
- }
- //设置分区的个数
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- //返回1代表1个分区
- return 1;
- }
- //设置行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- //返回1代表1行
- return 1;
- }
- //设置指定分区的头标标题
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- NSString *nss = @“头标题";
- return nss;
- }
- //设置指定分区的脚标标题
- - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
- NSString *nss = @"脚标题";
- return nss;
- }
- //设置cell 的行高
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- //默认行高44
- return 50;
- }
- //选中指定行 调用
- //已经选中
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSLog(@"第%d行选中",indexPath.row);
- }
- //取消选中的时候调用
- //从选中状态到非选中状态
- //反选
- - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSLog(@"第%d行取消选中/反选",indexPath.row);
- }
- 1 - (void)viewDidLoad
- 2 {
- 3 [super viewDidLoad];
- 4 //初始化数据
- 5 NSArray *[email protected][@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
- 6 NSArray *[email protected][@"李小龙",@"李小路"];
- 7 NSArray *[email protected][@"王刚"];
- 8 [email protected]{@"老张家":array1_, @"老李家":array2_, @"老王家":array3_};
- 9
- 10
- 11 UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
- 12 myTableView_.delegate=self;
- 13 myTableView_.dataSource=self;
- 14 //改变换行线颜色lyttzx.com
- 15 myTableView_.separatorColor = [UIColor blueColor];
- 16 //设定Header的高度,
- 17 myTableView_.sectionHeaderHeight=50;
- 18 //设定footer的高度,
- 19 myTableView_.sectionFooterHeight=100;
- 20 //设定行高
- 21 myTableView_.rowHeight=100;
- 22 //设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine
- 23 [myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
- 24 //设定cell分行线颜色
- 25 [myTableView_ setSeparatorColor:[UIColor redColor]];
- 26 //编辑tableView
- 27 myTableView_.editing=NO;
- 28
- 29 [self.view addSubview:myTableView_];
- 30
- 31 //跳到指的row or section
- 32 [myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]
- 33 atScrollPosition:UITableViewScrollPositionBottom animated:NO];
- 34
- 35 }
- 36
- 37
- 38
- 39 //指定有多少个分区(Section),默认为1
- 40 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- 41 return [[self.myDic allKeys] count];
- 42 }
- 43
- 44
- 45 //每个section底部标题高度(实现这个代理方法后前面 sectionHeaderHeight 设定的高度无效)
- 46 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- 47 return 20;
- 48 }
- 49
- 50 //每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效)
- 51 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- 52 return 20;
- 53 }
- 54
- 55 //每个section头部的标题-Header
- 56 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- 57 return [[self.myDic allKeys] objectAtIndex:section];
- 58 }
- 59
- 60 //每个section底部的标题-Footer
- 61 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
- 62 return nil;
- 63 }
- 64
- 65 //用以定制自定义的section头部视图-Header
- 66 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
- 67 return nil;
- 68 }
- 69
- 70 //用以定制自定义的section底部视图-Footer
- 71 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
- 72 UIImageView *imageView_=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
- 73 imageView_.image=[UIImage imageNamed:@"1000.png"];
- 74 return [imageView_ autorelease];
- 75 }
- 76
- 77
- 78 //指定每个分区中有多少行,默认为1
- 79 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- 80 return [[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:section]] count];
- 81 }
- 82
- 83 //改变行的高度(实现主个代理方法后 rowHeight 设定的高度无效)
- 84 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- 85 return 100;
- 86 }
- 87
- 88
- 89 //绘制Cell
- 90 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- 91 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
- 92
- 93 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- 94 SimpleTableIdentifier];
- 95
- 96 if (cell == nil) {
- 97 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- 98 reuseIdentifier: SimpleTableIdentifier] autorelease];
- 99 //设定附加视图
- 100 [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
- 101 // UITableViewCellAccessoryNone, // 没有标示
- 102 // UITableViewCellAccessoryDisclosureIndicator, // 下一层标示
- 103 // UITableViewCellAccessoryDetailDisclosureButton, // 详情button
- 104 // UITableViewCellAccessoryCheckmark // 勾选标记
- 105
- 106 //设定选中cell时的cell的背影颜色
- 107 cell.selectionStyle=UITableViewCellSelectionStyleBlue; //选中时蓝色效果
- 108 // cell.selectionStyle=UITableViewCellSelectionStyleNone; //选中时没有颜色效果
- 109 // cell.selectionStyle=UITableViewCellSelectionStyleGray; //选中时灰色效果
- 110 //
- 111 // //自定义选中cell时的背景颜色
- 112 // UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame];
- 113 // selectedView.backgroundColor = [UIColor orangeColor];
- 114 // cell.selectedBackgroundView = selectedView;
- 115 // [selectedView release];
- 116
- 117
- 118 // cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被选中
- 119
- 120 }
- 121
- 122 //这是设置没选中之前的背景颜色
- 123 cell.contentView.backgroundColor = [UIColor clearColor];
- 124 cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片
- 125 cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片
- 126 cell.textLabel.text=[[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
- 127 return cell;
- 128 }
- 129
- 130
- 131 //行缩进
- 132 -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
- 133 NSUInteger row = [indexPath row];
- 134 return row;
- 135 }
- 136
- 137 //选中Cell响应事件
- 138 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- 139 [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
- 140
- 141 //得到当前选中的cell
- 142 UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
- 143 NSLog(@"cell=%@",cell);
- 144 }
- 145
- 146 //行将显示的时候调用,预加载行
- 147 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- 148 {
- 149 NSLog(@"将要显示的行是\n cell=%@ \n indexpath=%@",cell,indexPath);
- 150 }
- 151
- 152 //选中之前执行,判断选中的行(阻止选中第一行)
- 153 -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- 154 {
- 155 NSUInteger row = [indexPath row];
- 156 if (row == 0)
- 157 return nil;
- 158 return indexPath;
- 159 }
- 160
- 161
- 162
- 163 //编辑状态,点击删除时调用
- 164 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
- 165 forRowAtIndexPath:(NSIndexPath *)indexPath
- 166 {
- 167
- 168 }
- 169
- 170 //cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法
- 171 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
- 172 NSLog(@"当前点击的详情button \n indexpath=%@",indexPath);
- 173 }
- 174
- 175 //右侧添加一个索引表
- 176 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
- 177 return [self.myDic allKeys];
- 178 }
- 179
- 180 //划动cell是否出现del按钮
- 181 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- 182 return YES;
- 183 }
- 184
- 185 //设定横向滑动时是否出现删除按扭,(阻止第一行出现)
- 186 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- 187 {
- 188 if (indexPath.row==0) {
- 189 return UITableViewCellEditingStyleNone;
- 190 }
- 191 else{
- 192 return UITableViewCellEditingStyleDelete;
- 193 }
- 194 return UITableViewCellEditingStyleDelete;
- 195 }
- 196
- 197 //自定义划动时delete按钮内容
- 198 - (NSString *)tableView:(UITableView *)tableView
- 199 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
- 200 return @"删除这行";
- 201 、、删除cell 时的动画效果
- 202 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- 203
- 204 }
- 205
- 206 //开始移动row时执行
- 207 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
- 208 {
- 209 NSLog(@"sourceIndexPath=%@",sourceIndexPath);
- 210 NSLog(@"sourceIndexPath=%@",destinationIndexPath);
- 211 }
- 212
- 213 //滑动可以编辑时执行
- 214 -(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
- 215 {
- 216 NSLog(@"willBeginEditingRowAtIndexPath");
- 217 }
- 218
- 219 //将取消选中时执行, 也就是上次先中的行
- 220 -(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
- 221 {
- 222 NSLog(@"上次选中的行是 \n indexpath=%@",indexPath);
- 223 return indexPath;
- 224 }
- 225
- 226
- 227 //让行可以移动
- 228 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- 229 {
- 230 return NO;
- 231 }
- 232
- 233 //移动row时执行
- 234 -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
- 235 {
- 236 NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
- 237 //用于限制只在当前section下面才可以移动
- 238 if(sourceIndexPath.section != proposedDestinationIndexPath.section){
- 239 return sourceIndexPath;
- 240 }
- 241 return proposedDestinationIndexPath;
- 242 }
- 243 //设置索引栏字体颜色
- _tableView.sectionIndexColor = [UIColor redColor];
来源: http://www.phpxs.com/code/1006483/