国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

UITableView多選Cell操作

2019-11-09 18:48:59
字體:
來源:轉載
供稿:網友

先新建一個項目,命名為UITableViewDemo,storyboard中拖入一個UITableViewController,然后新建一個繼承自UITableViewController的類,命名為UDTableViewController。在storyboard中將拖入的UITableViewController的Custom Class,Class設置為剛剛新建的UDTableViewController。

定義一個復用標識

static NSString * const reuseIdentifier = @"UDCell";

當前tableview注冊UITableViewCell類并設置復用標識為上面的標識

- (void)viewDidLoad {

[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier];

// Uncomment the following line to PReserve selection between presentations.

// self.clearsselectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.

self.navigationItem.rightBarButtonItem=self.editButtonItem;

}

采用復用的cell,并設置顯示內容

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

// Return the number of sections.

return 1;

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.

return 20;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];

cell.textLabel.text= [NSString stringWithFormat:@"%ld", indexPath.row];

// Configure the cell...

returncell;

}

現在看起來是這個樣子:

正常狀態編輯狀態

現在怎么讓UITableView編輯時為多選呢?

那就是實現UITableView的代理方法

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

UITableViewCellEditingStyle為編輯狀態, 聲明如下:

typedefNS_ENUM(NSInteger, UITableViewCellEditingStyle) {

UITableViewCellEditingStyleNone, //無

UITableViewCellEditingStyleDelete,//刪除狀態

UITableViewCellEditingStyleInsert//插入狀態

};

而多選就是UITableViewCellEditingStyleDelete、UITableViewCellEditingStyleInsert兩者的組合。

所以返回狀態如下:

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

{

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

現在可以多選了,效果圖:

多選

下面的方法為提交編輯時響應的方法,比如:刪除狀態下點擊cell右邊的刪除按鈕時;插入狀態下點擊左側添加按鈕時。編輯狀態下并不會調用此方法,所以只能取其它的辦法。

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

怎么保存多選的row呢?

如果要保存多選的row,我有一個方法(如果你有更好的方法,請告訴我

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄龙县| 祁门县| 三江| 郯城县| 黄山市| 金山区| 平顺县| 开封市| 龙州县| 如东县| 濮阳县| 信丰县| 高台县| 永宁县| 简阳市| 怀远县| 北宁市| 长顺县| 榆树市| 永川市| 颍上县| 仁布县| 兴城市| 望江县| 遂昌县| 邯郸县| 崇义县| 桂林市| 景洪市| 灵台县| 三亚市| 浦东新区| 涞源县| 石林| 恩平市| 牡丹江市| 柳江县| 贵州省| 富宁县| 黄龙县| 清丰县|