折腾给Your Second iOS App:BirdWatching添加地图的支持,然后继续参考:
Your Second iOS App - Next Steps – Add More Functionality的建议:
Make the master list editable and sortable. For some advice on how to enable these actions, see Table View Programming Guide for iOS. Also, recall that the Master-Detail template provides an Edit button (and some support for rearranging the table) by default. You might start on this task by removing the comment symbols around the appropriate code in
BirdsMasterViewController.m
.
去添加支持,使得列表可修改,可排序。
1.先去多参考:Table View Programming Guide for iOS,多学习点基础知识。
Table View Styles and Accessory Views中的内容总结:
2.table view的类是UITableView
,有两种基本类型:
- plain/regular:
- grouped:
3.UITableView含有:
- <必须的> Data Source
- [可选的] Delegate
4.除了row有header和footer之外,table view自己也可以有header和footer。
5.UITableViewCell有四种类型:
UITableViewCellStyleDefault:左边图片,右边Title
UITableViewCellStyleSubtitle:左边图片,右边:上面title,下面subtitle
UITableViewCellStyleValue1:左边Title,右边subtitle
UITableViewCellStyleValue2:左边subtitle,右边Title
注意,其中的叫法:
Title=main label
Subtitle=detail label
图片=image
6.和UITableViewCell相关的辅助信息,叫做Accessory Views,其有三种类型:
Standard accessory views | Description |
Disclosure indicator—UITableViewCellAccessoryDisclosureIndicator. You use the disclosure indicator when selecting a cell results in the display of another table view reflecting the next level in the data-model hierarchy. | |
Detail disclosure button—UITableViewCellAccessoryDetailDisclosureButton. You use the detail disclosure button when selecting a cell results in a detail view of that item (which may or may not be a table view). | |
Check mark—UITableViewCellAccessoryCheckmark. You use a checkmark when a touch on a row results in the selection of that item. This kind of table view is known as a selection list, and it is analogous to a pop-up list. Selection lists can limit selections to one row, or they can allow multiple rows with checkmarks. |
Overview of the Table View API的学习总结:
1.UITableView
是从UIScrollView
继承过来的,并且重定义了,只允许垂直滚动(vertical scrolling)。
2. 如上所说,UITableView必须有data source和delegate:
- data source:符合
UITableViewDataSource
协议,负责数据来源。包含两个必须的方法:tableView:numberOfRowsInSection:
:每个section显示几个rowtableView:cellForRowAtIndexPath:
:每个row里面显示几个cell
- delegate:符合
UITableViewDelegate
协议,负责逻辑控制。没有必须的方法。
所以,总的来说,和Table View相关的层次关系就是:
Table View > Section/Group > Row > Cell
3.NSIndexPath
之所以你在UITableView中常常看到NSIndexPath,那是因为,对于任意UITableView中的某个Cell的表示,是通过NSIndexPath来表示的。
其表示了Table中某个对象的index path,有了这个index path,就可以检索到对应的对象,包括UITableViewCell。
index path表示了某个树或内嵌的数组中的节点,而此种东西,在Foundation framework中,就是用NSIndexPath
来表示的。
所以,代码中常常看到NSIndexPath,通过此index path,去获得所选的UITableViewCell,然后进行相关的处理。
4.UITableViewCell
单个cell对应的对象为UITableViewCell。
其可以:
- 管理cell的选中
- 编辑cell
- accessory views
- 配置cell
Inserting and Deleting Rows and Sections的学习总结:
1. table view有两种模式:
注意:即使table view没有处在编辑模式,也可以实现插入和删除的动作(以及相关的动画效果)。
Managing the Reordering of Rows中解释了如何改变cell位置,即实现可排序,且给出了示例代码。
现在去照葫芦画瓢试试,去实现让table list实现可调换位置,具体折腾过程参考:
【已解决】如何实现iOS中的UITableViewCell的重排,即如何实现moveRowAtIndexPath函数
1.突然发现,实现了上述的可以手动编辑调换位置,就是editable和sortable了。
2.不过发现一个小问题,就是点击Delete的时候,无法删除:
所以,继续去解bug。
3.后来是把commitEditingStyle的代码取消注释,然后修改一下变量,即可:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.dataController.masterBirdSightingList removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } }
即可支持删除操作了。
至此,好像就是支持editable和sortable了。