当前位置:首页 >娱乐 >iOS可折叠的tableView的简单实现 的简单实需要努力和坚韧

iOS可折叠的tableView的简单实现 的简单实需要努力和坚韧

2024-06-29 07:32:20 [百科] 来源:避面尹邢网

iOS可折叠的折叠tableView的简单实现

作者:xling 移动开发 iOS 漫漫编程路,刚接触iOS编程的的简单实你做好准备了么。凡事从无到有,折叠由简至繁,的简单实需要努力和坚韧,折叠那么从最简单的的简单实积累开始吧。

这里所说的折叠 tableView 折叠,是的简单实借助 insert (insertRowsAtIndexPaths:withRowAnimation:) 和 delete (deleteRowsAtIndexPaths:withRowAnimation:) 对tableView 的 section 进行折叠,效果简单。折叠

我是的简单实初学者,还请各位看官指教。折叠

iOS可折叠的tableView的简单实现 的简单实需要努力和坚韧

搞这个东西,的简单实断断续续了几天,折叠现在终于可以告一段落了,的简单实虽然离可重用还有距离,折叠但是我想以经不远了。

iOS可折叠的tableView的简单实现 的简单实需要努力和坚韧

遇到的几个问题:

iOS可折叠的tableView的简单实现 的简单实需要努力和坚韧

1.NSMutableDictionary

NSMutableDictionary 内的顺序并不是执行 setObjectForKey 的顺序,顺序是乱的(目测是乱的,不知道是不是有什么规律,请指教)。

我在这个东西上绕了一个大圈子。后来在 AppCode  下面调试,才发现这个问题。为保持插入顺序,不得不换成两个 NSMutableArray ,一个存 Key,一个存 Value.

2.headerViewForSection

我想在 numberOfRowsInSection 方法内获取该 section 的 header View ,然后读取自定义的数据,比如当前状态是否是折叠等。

查了一下API,发现有个 headerViewForSection 方法,但是无论如何,它的值一直都是nil,搞不明白。stackoverflow 上有说须要在使用之前用 tableView 的 registerNib:forHeaderFooterViewReuseIdentifier:  或 registerClass:forHeaderFooterViewReuseIdentifier:  方 法,我试了,没用。

3.insertRowsAtIndexPaths 和 deleteRowsAtIndexPaths 同 numberOfRowsInSection 的关系

如果不处理好这个关系,大概所有的问题都是这样的:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 6. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 3 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 4 which only contains 0 rows before the update'

大楖是说,numberOfRowsInSection 返回的数字和 insert / delete 后,section 所包含的行数不一致。

在 tableView 的 endUpdates 方法之前,应该对标志变量做处理。endUpdates 后,会重新刷新 tableView ,numberOfRowsInSection 会重新被调用。

不理想的地方(坏代码)

同上面讲的1和2 ,由于 NSMutableDictionary 的无顺序,我将 MutableDictionary 换成了 MutableArray 。

由于无法获取指定 section 的 headerView ,所以,我只能将以经生成的 headerView 记录到某个 MutableArray 内,以供使用。

用 xib 设计自定义的 UIView

在新建 Objective-C Class 的时候,只有基类是 UIViewController 的,才能选“With XIB for user interface” 这个选项。

如果我想自定义一个 UIView , 这个 view 内的控件很多,想想就是一件可怕的事情。还好,可以单独新建一个 xib 文件。

a, 拖一个UIView 到 xib 编辑器中,然后更发这个 view 的size 为 Freeform, 这个 view 就可以自由的改变大小了。

b,更改 class 为自定义的 UIView 

c, File's Owner 的 class 还是 NSObject ,不要改。

d, 如何使用这个自定义的 UIView ?

***,上个效果图吧

责任编辑:闫佳明 来源: cnblogs iOS开发可折叠tableView

(责任编辑:知识)

    推荐文章
    热点阅读