步驟
- 模型數(shù)據(jù)從
NSArray
->NSMutableArray
- 在故事板(Storyboard)中添加 增加按鈕 和 刪除按鈕
添加按鈕
- (IBAction)add {
// tableView里面需要顯示新的cell數(shù)據(jù)赦肃,只需要操作模型數(shù)據(jù)
XMGDeal *deal = [[XMGDeal alloc] init];
deal.title = [NSString stringWithFormat:@"XX飯店大打折 %d折", arc4random_uniform(50)];
deal.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
deal.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
deal.icon = @"5ee372ff039073317a49af5442748071";
[self.deals insertObject:deal atIndex:0];
XMGDeal *deal2 = [[XMGDeal alloc] init];
deal2.title = [NSString stringWithFormat:@"YY飯店大打折 %d折", arc4random_uniform(50)];
deal2.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
deal2.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
deal2.icon = @"5ee372ff039073317a49af5442748071";
[self.deals insertObject:deal2 atIndex:0];
// 提醒tabelView,模型數(shù)據(jù)發(fā)生了變化,請重新識別床估,請重新向數(shù)據(jù)源索要數(shù)據(jù)
[self.tableView reloadData];
// 插入某些特定的行
// [self.tableView insertRowsAtIndexPaths:@[
// [NSIndexPath indexPathForRow:0 inSection:0],
// [NSIndexPath indexPathForRow:1 inSection:0]
// ] withRowAnimation:UITableViewRowAnimationLeft];
}
刪除按鈕
- (IBAction)remove {
// 移除模型數(shù)據(jù)
[self.deals removeObjectAtIndex:0];
[self.deals removeObjectAtIndex:0];
[self.deals removeObjectAtIndex:0];
// 刷新表格
[self.tableView reloadData];
// [self.tableView deleteRowsAtIndexPaths:@[
// [NSIndexPath indexPathForRow:0 inSection:0],
// [NSIndexPath indexPathForRow:1 inSection:0],
// [NSIndexPath indexPathForRow:2 inSection:0]
// ] withRowAnimation:UITableViewRowAnimationRight];
// 15個cell:13
// 15個模型:12
}
更新數(shù)據(jù)
- (IBAction)update {
// XMGDealCell *cell = (XMGDealCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
// cell.priceLabel.text = @"¥999";
//
// return;
// 修改模型
XMGDeal *deal = self.deals[3];
deal.price = [NSString stringWithFormat:@"%d", 50 + arc4random_uniform(100)];;
// 刷新表格
[self.tableView reloadData];
// [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
}
進(jìn)入編輯模式
- (IBAction)switchEditing {
// 進(jìn)入編輯模式
// self.tableView.editing = YES;
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
左劃刪除-編輯模式
#pragma mark - TableView代理方法
/**
* 只要實現(xiàn)這個方法尿孔,左劃cell出現(xiàn)刪除按鈕的功能就有了
* 用戶提交了添加(點(diǎn)擊了添加按鈕)\刪除(點(diǎn)擊了刪除按鈕)操作時會調(diào)用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 點(diǎn)擊了“刪除”
// 刪除模型
[self.deals removeObjectAtIndex:indexPath.row];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
} else if (editingStyle == UITableViewCellEditingStyleInsert) { // 點(diǎn)擊了+
NSLog(@"+++++ %zd", indexPath.row);
}
}
/**
* 這個方法決定了編輯模式時鲁猩,每一行的編輯類型:insert(+按鈕)适滓、delete(-按鈕)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}
添加按鈕彈出框
- UIAlertView 彈出框方法
- IOS8之后方法:UIAlertController
- (IBAction)add {
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"111" message:@"2222" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//
// [alertView show];
// UIActionSheet *sheet;
// 創(chuàng)建彈框控制器
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請輸入團(tuán)購信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 添加按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 創(chuàng)建模型
XMGDeal *deal = [[XMGDeal alloc] init];
deal.title = [alert.textFields[0] text];
deal.price = [alert.textFields[1] text];
[self.deals insertObject:deal atIndex:0];
// 刷新數(shù)據(jù)
[self.tableView reloadData];
}]];
// [alert addAction:[UIAlertAction actionWithTitle:@"不知道" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// NSLog(@"點(diǎn)擊了不知道按鈕");
// }]];
// [alert addAction:[UIAlertAction actionWithTitle:@"不知道2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// NSLog(@"點(diǎn)擊了不知道2按鈕");
// }]];
// 添加文本輸入框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入團(tuán)購名字";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入團(tuán)購價格";
}];
// 顯示控制器
[self presentViewController:alert animated:YES completion:nil];
}
批量操作
- 步驟
- 給check選中按鈕添加到模型數(shù)據(jù)中
@property (assign, nonatomic, getter=isCheck) BOOL check;
- 連接輸出接口澳窑,并設(shè)置默認(rèn)隱藏
@property (weak, nonatomic) IBOutlet UIImageView *check;
self.check.hidden = !tg.isCheck;
- 調(diào)用代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 按鈕方法
- (IBAction)remove {
// 臨時數(shù)組:存放即將需要刪除的團(tuán)購數(shù)據(jù)
NSMutableArray *deletedTgs = [NSMutableArray array];
for (YFtg *tg in self.tgs) {
if (tg.isCheck) [deletedTgs addObject:tg];
}
// 刪除模型數(shù)據(jù)
[self.tgs removeObjectsInArray:deletedTgs];
[self.tableView reloadData];
}
// 代理方法:選中被刪除方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 點(diǎn)擊不被選中
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
// 默認(rèn)隱藏皂甘,所以取反 打鉤
YFtg *tg = self.tgs[indexPath.row];
tg.check = !tg.isCheck;
// 更新數(shù)據(jù)
[self.tableView reloadData];
}