最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

给Your Second iOS App的BirdWatching添加让列表可修改,可排序

iOS crifan 2315浏览 0评论

折腾给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有四种类型:

注意,其中的叫法:

Title=main label

Subtitle=detail label

图片=image

 

6.和UITableViewCell相关的辅助信息,叫做Accessory Views,其有三种类型:

Standard accessory views

Description

disclosure_indicator

Disclosure indicatorUITableViewCellAccessoryDisclosureIndicator. 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.

UIButtonTypeDetailDisclosure

Detail disclosure buttonUITableViewCellAccessoryDetailDisclosureButton. 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

Check markUITableViewCellAccessoryCheckmark. 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:

 

所以,总的来说,和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有两种模式:

  • 正常(normal)/选择(selection)模式:
  • 编辑(editing)模式:row的右边,会有指示,包含两种:
    • Deletion control deletion_control:删除
    • Insertion control insertion_control:插入

注意:即使table view没有处在编辑模式,也可以实现插入和删除的动作(以及相关的动画效果)。


Managing the Reordering of Rows中解释了如何改变cell位置,即实现可排序,且给出了示例代码。

现在去照葫芦画瓢试试,去实现让table list实现可调换位置,具体折腾过程参考:

【已解决】如何实现iOS中的UITableViewCell的重排,即如何实现moveRowAtIndexPath函数


1.突然发现,实现了上述的可以手动编辑调换位置,就是editable和sortable了。

2.不过发现一个小问题,就是点击Delete的时候,无法删除:

cannot 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了。

转载请注明:在路上 » 给Your Second iOS App的BirdWatching添加让列表可修改,可排序

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.166 seconds, using 22.21MB memory