新手哈,第一次寫技術(shù)文章饭尝,希望各位看官多多指教。
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,這個(gè)時(shí)候的section的header是浮在上面的姊途,滑動(dòng)的時(shí)候在頂部是固定的涉瘾,當(dāng)然,這個(gè)時(shí)候也可以添加個(gè)所以列表吭净,類似于通訊錄的26個(gè)字母的索引睡汹。當(dāng)然,有時(shí)候也用到group寂殉,一般在設(shè)置里用的比較多囚巴,這個(gè)時(shí)候就沒有索引了。
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:.
這個(gè)是tableview的編輯模式友扰,可以是刪除彤叉,也可以是添加。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert;
}
用這個(gè)去選擇你要的是刪除還是添加村怪。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}
在這個(gè)回調(diào)里去添加你所希望發(fā)生的事件秽浇,當(dāng)用刪除的時(shí)候,可以用:
[tableView selectRowAtIndexPath:<#(nullable NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>]
選擇你所需要?jiǎng)h除的indepath甚负,當(dāng)然柬焕,于此同時(shí),你要對(duì)數(shù)據(jù)源的數(shù)組進(jìn)行處理梭域,不然會(huì)蹦的思密達(dá)~
同理斑举,要添加也是一樣的:
[tableView insertSections:<#(nonnull NSIndexSet *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
當(dāng)然,很多同學(xué)剛剛接觸的時(shí)候病涨,會(huì)發(fā)現(xiàn)即使你只是設(shè)置了十個(gè)row富玷,但是下面的橫線密密麻麻的。既穆。赎懦。很多。有個(gè)小技巧就是你可以這樣設(shè)置:
_tableView.tableFooterView = [[UIView alloc] init];
當(dāng)然幻工,更多時(shí)候我們做的是對(duì)于datesource的處理励两,一般都是reloaddata。
這邊有一個(gè)小小的demo囊颅,用于tableview的movers
https://github.com/ioscick/LongpressTableViewCellDemo