最近学习了uitableviewcontroller控制器以及uitableview使用。今天就来总结下。
-uitableview基本使用
首先来分析下uitableview:iOS中的tableview是可以分区的,每个区对应的行数;思考每一行显示的内容;用户选择一行所触发的事件。以上几个因素所设计到
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//tableview 分区
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //对应区指定的行数
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//在此方法中实现cell、内容填充。
另注:NSIndexPath中已经包含对应的uitableviewcell(uitableview的子控件)的section和row。
-uitableviewcell style
[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”cell”];中需要知道uitableviewcell的style,cell共有4个style:
-UITableViewCellStyleDefault:
-UITableViewCellStyleSubtitle:
-UITableViewCellStyleValue1:
-UITableViewCellStyleValue2:
以上4个style大家可以多试试,便知道他们的区别。
-分区
对于uitableview 分区之间留有间隔,直接在右边style选择grouped。
- uitableviewcell的三个要素:
textlabel-uilable :cell上显示文字
detailtextlabel -uilabel:显示cell的详细内容
imageview-uiimageview用于显示cell上的图片信息。
-cell重用
uitableview界面中会有很多的cell,用过的cell并不会释放,而是放入了tableview的队列。因此在创建cell对象之前,先试着从队列中获取已存在的cell对象,if能拿到,则服用cell对象。
-刷新uitableview
[tableview reloadData]
-tableview 编辑模式
编辑模式即对uitableviewcell增、删、移动。
设置编辑模式 [self.tableView setEditing:boolean animated:YES];
-对cell编辑操作设置以及操作:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath //对于可否编辑设置
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath //在此方法中实现增、删
--cell移动
对于cell的移动无非对数据的顺序进行改变:先删除此数据并在指定位置增加上此数据。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
-uitableviewcell中的accessoryview操作
默认情况下accessoryview是nil。通常情况下accessory view有4种style:UITableViewCellAccessoryNone,
UITableViewCellAccessoryDisclosureIndicator,
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark。其中 UITableViewCellAccessoryDetailDisclosureButton是显示i,点击会触发:
(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)
当然accessoryview也可以自己定义:即 cell.accessoryView=自定义view。
想到的就那么多,我自己写的uitablviewcontroller的使用代码下载地址:uitableview使用的demo下载。
文中可能有很多的错误,望指正。谢谢。