UITableView
来源:互联网 发布:象屿集团怎么样知乎 编辑:程序博客网 时间:2024/05/16 05:48
一、TableView创建和基本设置
<h4>1. 初始化</h4> UIView的四步创建<h4>2. 设置行高</h4> // 设置行高 tableView.rowHeight = 100;<h4>3.签协议,设置代理人</h4> tableView.dataSource = self; tableView.delegate = self;<h4>4.实现协议方法</h4> 1).必须实现:指定每个分区多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.arr.count; } 2).必须实现:让TableView显示内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.先指定一个cell的重用标志 static NSString *reuse = @"reuse"; // 创建并查找cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; // 如果没找到,对应的cell是0 if(!cell){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } // cell提供了三种视图,两个Label,一个imageView cell.textLabel.text = self.arr[indexPath.row]; cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section]; cell.imageView.image = [UIImage imageNamed:@"1.jpg"];// NSLog(@"%ld",indexPath.row); return cell;} 3.)设置分区的悬浮标题 // 头- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSString *str = [NSString stringWithFormat:@"%ld",section + 1]; return str;} // 尾 titleForFooterInSection 4).设置tableView里有几个分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4;} 5.) 设置屏幕右边的快捷条- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"0",@"1",@"2",@"3"];} 6). 用来动态的设置行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row % 2 == 0){ return 150; } else{ return 100; }}
二、UITableView编辑
<h4>1.随意的移动Cell</h4> 只要写了这个方法就可以随意的移动cell但是它只是视觉上的移动,并没有改变数据的顺序,要想改变数据的顺序,需要用代码来实现 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 获取要移动的数据 Student *stu = [self.stuArr[sourceIndexPath.row] retain]; // 在数组里将这个对象移除掉 [self.stuArr removeObjectAtIndex:sourceIndexPath.row]; // 将移动的数据插入到 [self.stuArr insertObject:stu atIndex:destinationIndexPath.row]; [stu release];}<h4>2.设置编辑键</h4> 1.)开启每一个Cell都可编辑 [self.tableView setEditing:YES animated:YES]; 2.)逐行的设置是否可编辑(在协议方法中)- (BOOL)tableView:(UITableView *) tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row % 2 == 0){ return YES; } else{ return NO; }} 3.) 设置编辑模式(删除,编辑,多选)- (UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath{return // 返回删除状态 UITableViewCellEditingStyleDelete; // 返回编辑状态 // UITableViewCellEditingStyleInsert // 返回多选状态 // UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete} 4.)设置编辑按钮的标题(可以对每一个按钮进行设置)- (NSString *)tableView: (UITableView *)tableViewtitleForDeleteConfirmationButtonForRowAtIndexPath :(NSIndexPath *)indexPath{ return @"你点呀”}<h4>3.设置编辑状态和导航视图的右边按钮绑定</h4> 点击按钮可以改变Cell是否可编辑1.) 设置导航栏的编辑按钮 self.navigationItem.rightBarButtonItem = self.editButtonItem;2.)绑定起来 重写系统提供的编辑按钮的点击方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; // 让TableView的编辑状态随着上面rightButton的状态一致 // 这里使关键 [self.tableView setEditing:editing animated:animated]}<h4>4.设置左划出现编辑键</h4> 左划出现编辑键,同时还有完成点击方法// 在界面上左划,划出编辑按钮,点击按钮的方法- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // 实现点击方法 NSLog(@"编辑开始"); // 先判断编辑模式 if(editingStyle == UITableViewCellEditingStyleDelete){ // 先把数组里的对象删除掉 [self.stuArr removeObjectAtIndex:indexPath.row]; // 单行刷新 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; }}<h4>5.设置多个编辑键</h4>- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ // 新建第一个按钮 UITableViewRowAction *actionFirst = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"通话" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { // 点击编辑键触发的方法 NSLog(@"Block里面"); }]; // 设置颜色 actionFirst.backgroundColor = [UIColor brownColor]; // 新建第二个编辑键 UITableViewRowAction *actionSecond = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:str handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { // 触发了点击方法 [self rowAction:actionFirst]; NSLog(@"点击了第二个"); }]; actionSecond.backgroundColor = [UIColor blueColor]; // 返回编辑键组成的数组 return @[actionFirst,actionSecond];}
三、补充
<h4>1.去掉滑动的时候出现的分割线(去掉分割线)</h4> self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 去掉下面没有元素的分割线 self.tableView.tableFooterView = [[UIView alloc] init];<h4>2.去掉滚动条</h4> self.proTableView. showsVerticalScrollIndicator = NO;<h4>3.判断TableView是哪一个</h4> 1.)将TableView设置为属性,直接比较地址 if(self.proTableView == tableView){} 2.)通过设置Tag值来判断 if(self.proTableView.tag = 10001){}<h4>4.TableViewCell的单行刷新</h4>[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation: UITableViewRowAnimationFade];<h4>5.在TableView上面添加一个视图(轮播图,广告)</h4>1.)在TableView上添加一个头视图 self.tableView.tableHeaderView = imageView;2.) 通过坐标的设置// 平移当前的TableView的位置 self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);// ImageView的位置相对应的要改变self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, -200)]<h4>6.设置图片随着拉动而变大</h4> // 在TableView中写ScrollView的协议方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ // 根据当前的偏移量,实时的设置ImageView的高度,形成图片随着拉动放大的效果 CGFloat yOffest = self.tableView.contentOffset.y; NSLog(@"%f",yOffest); if(self.tableView.contentOffset.y < 0){ self.imageView.frame = CGRectMake(0, yOffest, self.tableView.frame.size.width, - yOffest); }}
0 0
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableview
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView,
- UITableView
- UITableView
- UITableView
- UITableView
- 将博客搬至CSDN
- bzoj2456: mode
- 自定义复选框,复选框样式修改,重写复选框
- eclipse+webservice开发实例
- fileters文件不起作用 原因
- UITableView
- JavaScript将数字转换为大写金额
- Laravel 使用遇到的问题及解决方法
- BF算法
- jqueryeasyui三级联动
- 安卓系统权限如何工作
- Android Studio--Plugin is too old...
- C语音 指针
- 微信上如何发送GIF动图