在前面的文章里面已经写过了“UITableView基础”,所以这篇里面就不再对这里面的内容进行介绍。如果有幸去瞄一眼的,可以通过文章下面的拓展链接传送门去看。&
今天重点聊一聊UITableView中经常使用到的代理方法及属性。本文不是工具箱,所以不会将所有的属性和方法都写下来噢。只是总结经常使用到的。太完整的也记不住,真的是要用的时候临时翻一下.h文件看看也行。
属性名称 | 数值 | 作用 |
---|---|---|
separatorStyle | UITableViewCellSeparatorStyle | 分割线样式 |
separatorColor | UIColor | 分隔线颜色 |
属性名称 | 数值 | 作用 |
---|---|---|
allowsSelection | BOOL | 允许选中 |
allowsMultipleSelection | BOOL | 允许多选 |
indexPathsForSelectedRows | NSArray < NSIndexPath *> | 获取当前选中cell的indexPaths |
indexPathsForVisibleRows | NSArray < NSIndexPath *> | 当前可见行数 |
allowsSelection
:BOOL类型,一是说说这一个cell是否可以被选中。在某种情况下,我们希望点击cell的时候不需要做出任何的反应,就可以修改这个属性。allowsMultipleSelection
:需要进行多行选择的时候就要将此设置为YES
。indexPathsForSelectedRows
:这里返回的是包含了indexPath
的数组噢,因为要考虑到是多行选中的情况。知道了这个属性之后,不要一说获取选中的cell的indexPath就只会用代理方法。& indexPathsForVisibleRows
:这个方法其实并不太经常使用,但是很能提升逼格。这个属性也是一个数组,它装着目前屏幕上可见的cell的indexPath集合。在做两级菜单联动的时候可能会需要用到。神马滚动到指定的cell,设置cell的高度,设置header、footer的高度等等这些方法就不再说了。
//选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Paste_Image.png
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
//修改上图的图标
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
重要:开启支持拖拽排序的前提是:开启支持编辑模式。
Paste_Image.png
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
```
//修改删除按钮文字
##2.4 自定义cell左滑事件
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2248583-728eb64caa005450.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##2.5 修改cell左滑文字
#3. 四种刷新tableView的方法
// 新增表格数据
[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];
// 删除表格数据
[tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
// 局部刷新指定的行
[tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
// 刷新全部表格数据,如果能够准确确定被修改的数据行,就不要用此方法
[tableView reloadData];
#4. tableViewCell排序
##4.1 cell交换排序
- 在cell拖拽对应的执行方法中进行。
[self.contactArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
##4.2 cell顺序排序
- 依旧还是在cell拖拽对应的执行方法中进行。
// 获取选中的数组。删除后,插入到相应的行
GMContact *tempContact = self.contactArray[fromIndexPath.row];
[self.contactArray removeObjectAtIndex:fromIndexPath.row];
[self.contactArray insertObject:tempContact atIndex:toIndexPath.row];
##4.3 开了编辑模式后,在编辑模式下插入一条cell
- 需要在编辑模式下,修改icon执行方法中写入。