在tableView的使用過程中,有時(shí)會需要增加或刪除某些cell數(shù)據(jù).不管是添加還是刪除,我們都應(yīng)該操作
模型
數(shù)據(jù),因?yàn)閏ell是會被重新利用的,如果直接對cell進(jìn)行修改,會導(dǎo)致重用后數(shù)據(jù)錯(cuò)亂,造成不必要的麻煩.
增加cell數(shù)據(jù)
在控制器中添加一個(gè)按鈕,改名為添加,拖線到實(shí)現(xiàn)代碼中,用來監(jiān)聽點(diǎn)擊事件.
添加方法需要記錄用戶輸入的內(nèi)容,添加到模型中,然后再刷新tableView即可.
在方法中實(shí)現(xiàn)以下內(nèi)容:
- (IBAction)add:(id)sender {
//創(chuàng)建消息控制器
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請輸入團(tuán)購數(shù)據(jù)" message:nil preferredStyle:UIAlertControllerStyleAlert];
//添加輸入文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入標(biāo)題";//設(shè)置占位文本
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入價(jià)格";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入購買數(shù)";
}];
//添加操作按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//創(chuàng)建模型
DLShop *shop = [[DLShop alloc] init];
//設(shè)置模型數(shù)據(jù)
shop.title = [alert.textFields[0] text];
shop.price = [alert.textFields[1] text];
shop.buyCount = [alert.textFields[2] text];
//將新數(shù)據(jù)添加到數(shù)組
[self.shops insertObject:shop atIndex:0];
//刷新表格(可視行,不帶動畫)
// [self.tableView reloadData];
//刷新表格(單行,帶動畫,性能優(yōu)化)
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}]];
//顯示消息框
[self presentViewController:alert animated:YES completion:nil];
}
刪除cell數(shù)據(jù)(單行,系統(tǒng)自帶)
在控制器中添加一個(gè)按鈕,改名為刪除,拖線到實(shí)現(xiàn)代碼中,用來監(jiān)聽點(diǎn)擊事件.
系統(tǒng)自帶的刪除方法實(shí)現(xiàn)比較簡單,只需實(shí)現(xiàn)下面的方法,然后刷新tableView即可.
代碼如下:
* 只要實(shí)現(xiàn)這個(gè)方法温算,左劃cell出現(xiàn)刪除按鈕的功能就有了
* 用戶提交了添加(點(diǎn)擊了添加按鈕)\刪除(點(diǎn)擊了刪除按鈕)操作時(shí)會調(diào)用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//刪除模型數(shù)據(jù)
[self.shops removeObjectAtIndex:indexPath.row];
//刪除對應(yīng)行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
刪除cell數(shù)據(jù)(多行)
在控制器中添加一個(gè)按鈕,改名為刪除,拖線到實(shí)現(xiàn)代碼中,用來監(jiān)聽點(diǎn)擊事件.
刪除cell比較簡單,只需將選中的數(shù)據(jù)記錄到可變數(shù)組,從模型中刪除可變數(shù)組內(nèi)容,然后刷新tableView即可.
代碼如下:1.在模型中添加一個(gè)狀態(tài)屬性
/** 狀態(tài)量標(biāo)識有無被打鉤 */
@property (assign, nonatomic, getter=isChecked) BOOL checked;
2.在cell中添加一張圖片,設(shè)置圖片的內(nèi)容為對勾,并設(shè)置圖片默認(rèn)隱藏.將圖片拖線至自定義類中,修改set方法,設(shè)置圖片的顯示狀態(tài)(顯示或隱藏)
// 設(shè)置打鉤控件的顯示和隱藏
self.checkView.hidden = !deal.isChecked;
3.設(shè)置remove方法
- (IBAction)remove:(id)sender {
// 臨時(shí)數(shù)組:存放即將需要刪除的團(tuán)購數(shù)據(jù)
NSMutableArray *deleteShops = [NSMutableArray array];
//遍歷模型數(shù)據(jù),取出被選中的數(shù)據(jù)
for (DLShop *shop in self.shops) {
//將數(shù)據(jù)添加到可變數(shù)組中
if (shop.isChecked) [deleteShops addObject:shop];
}
//刪除可變數(shù)組的模型數(shù)據(jù)
[self.shops removeObjectsInArray:deleteShops];
//刷新表格
[self.tableView reloadData];
//清空可變數(shù)組
[deleteShops removeAllObjects];
}
刪除cell數(shù)據(jù)(多行,系統(tǒng)自帶)
首先添加一個(gè)批量操作按鈕,拖到實(shí)現(xiàn)代碼中,監(jiān)聽點(diǎn)擊事件,
實(shí)現(xiàn)如下代碼:
1.
- (void)viewDidLoad {
[super viewDidLoad];
// 允許在編輯模式進(jìn)行多選操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
2.在批量操作按鈕方法下實(shí)現(xiàn):
- (IBAction)multiOperation:(id)sender {
//對選中狀態(tài)進(jìn)行取反
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
3.在刪除按鈕實(shí)現(xiàn)以下代碼:
- (IBAction)remove {
// 獲得所有被選中的行
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
// 遍歷所有的行號
NSMutableArray *deletedShops = [NSMutableArray array];
for (NSIndexPath *path in indexPaths) {
[deletedShops addObject:self.shops[path.row]];
}
// 刪除模型數(shù)據(jù)
[self.shops removeObjectsInArray:deletedShops];
// 刷新表格 一定要刷新數(shù)據(jù)
[self.tableView reloadData];
}