美文网首页程序员
iOS UITableview preference

iOS UITableview preference

作者: __夏至未至 | 来源:发表于2016-04-11 15:15 被阅读154次

    新手哈,第一次写技术文章,希望各位看官多多指教。

    Table views can have one of two styles,UITableViewStylePlainandUITableViewStyleGrouped. When you create aUITableViewinstance you must specify a table style, and this style cannot be changed. In the plain style, section headers and footers float above the content if the part of a complete section is visible. A table view can have an index that appears as a bar on the right hand side of the table (for example, "A" through "Z"). You can touch a particular label to jump to the target section. The grouped style of table view provides a default background color and a default background view for all cells. The background view provides a visual grouping for all cells in a particular section. For example, one group could be a person's name and title, another group for phone numbers that the person uses, and another group for email accounts and so on. See the Settings application for examples of grouped tables. Table views in the grouped style cannot have an index.

    很显然,这是tableview的两种形式(基本都知道),大部分用的是plain,这个时候的section的header是浮在上面的,滑动的时候在顶部是固定的,当然,这个时候也可以添加个所以列表,类似于通讯录的26个字母的索引。当然,有时候也用到group,一般在设置里用的比较多,这个时候就没有索引了。

    When sent asetEditing:animated:message (with a first parameter ofYES), the table view enters into editing mode where it shows the editing or reordering controls of each visible row, depending on theeditingStyleof each associatedUITableViewCell. Clicking on the insertion or deletion control causes the data source to receive atableView:commitEditingStyle:forRowAtIndexPath:message. You commit a deletion or insertion by callingdeleteRowsAtIndexPaths:withRowAnimation:orinsertRowsAtIndexPaths:withRowAnimation:, as appropriate. Also in editing mode, if a table-view cell has itsshowsReorderControlproperty set toYES, the data source receives atableView:moveRowAtIndexPath:toIndexPath:message. The data source can selectively remove the reordering control for cells by implementingtableView:canMoveRowAtIndexPath:.

    这个是tableview的编辑模式,可以是删除,也可以是添加。

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleInsert;

    }

    用这个去选择你要的是删除还是添加。

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    }

    在这个回调里去添加你所希望发生的事件,当用删除的时候,可以用:

    [tableView selectRowAtIndexPath:<#(nullable NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>]

    选择你所需要删除的indepath,当然,于此同时,你要对数据源的数组进行处理,不然会蹦的思密达~

    同理,要添加也是一样的:

    [tableView insertSections:<#(nonnull NSIndexSet *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

    当然,很多同学刚刚接触的时候,会发现即使你只是设置了十个row,但是下面的横线密密麻麻的。。。很多。有个小技巧就是你可以这样设置:

    _tableView.tableFooterView = [[UIView alloc] init];

    当然,更多时候我们做的是对于datesource的处理,一般都是reloaddata。

    这边有一个小小的demo,用于tableview的movers

    https://github.com/ioscick/LongpressTableViewCellDemo

    相关文章

      网友评论

        本文标题:iOS UITableview preference

        本文链接:https://www.haomeiwen.com/subject/oddilttx.html