亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

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

IOS之表視圖單元格刪除、移動及插入

2019-11-14 19:41:42
字體:
來源:轉載
供稿:網友

 

1.實現單元格的刪除,實現效果如下

   

Cpp代碼 復制代碼 收藏代碼
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //設置導航欄  
  5.      self.editButtonItem.title = @"編輯";  
  6.     self.navigation.rightBarButtonItem = self.editButtonItem;  
  7.     [self initTableViewData];  
  8.     // Do any additional setup after loading the view.  
  9. }  
  10.   
  11. - (void)didReceiveMemoryWarning  
  12. {  
  13.     [super didReceiveMemoryWarning];  
  14.     // Dispose of any resources that can be recreated.  
  15. }  
  16.   
  17. -(void)initTableViewData{  
  18.     NSBundle *bundle = [NSBundle mainBundle];  
  19.     NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];  
  20.     dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];  
  21. }  
  22.   
  23. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  24. {  
  25.     return [dataArr count];  
  26. }  
  27.   
  28. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  29. {  
  30.     static NSString *CellIdentifier = @"tableCell";  
  31.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  32.       
  33.     NSUInteger row = [indexPath row];  
  34.     NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  35.     cell.textLabel.text =  [rowDict objectForKey:@"itemName"];  
  36.     NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);  
  37.       
  38.     NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];  
  39.     cell.imageView.image = [UIImage imageNamed:imagePath];  
  40.     NSLog(@"cell.image.image  =  %@",imagePath);  
  41.       
  42.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  43.       
  44.     return cell;  
  45. }  
  46.   
  47. //選中Cell響應事件  
  48. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  49.     [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中后的反顯顏色即刻消失  
  50.     NSUInteger row = [indexPath row];  
  51.     NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  52.     NSString *userName =  [rowDict objectForKey:@"itemName"];  
  53.     NSLog(@"userName=%@",userName);  
  54. }  
  55.   
  56. //返回編輯狀態的style  
  57. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  
  58.            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  59. {  
  60.     //UITableViewCellEditingStyleInsert  
  61. //    return UITableViewCellEditingStyleNone;  
  62.     return UITableViewCellEditingStyleDelete;  
  63. }  
  64. //完成編輯的觸發事件  
  65. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
  66. forRowAtIndexPath:(NSIndexPath *)indexPath  
  67. {  
  68.     if (editingStyle == UITableViewCellEditingStyleDelete)  
  69.     {  
  70.         [dataArr removeObjectAtIndex: indexPath.row];  
  71.         //        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  72.         //                         withRowAnimation:UITableViewRowAnimationFade];  
  73.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  74.                          withRowAnimation:UITableViewRowAnimationFade];  
  75.         [tableView reloadData];  
  76.     }   
  77. }  
  78. //UIViewController生命周期方法,用于響應視圖編輯狀態變化  
  79. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {  
  80.     [super setEditing:editing animated:animated];  
  81.       
  82.     [self.tableView setEditing:editing animated:YES];  
  83.     if (self.editing) {  
  84.      self.editButtonItem.title = @"完成";  
  85.     } else {  
  86.         self.editButtonItem.title = @"編輯";  
  87.     }  
  88. }  
  89. @end  
- (void)viewDidLoad{    [super viewDidLoad];    //設置導航欄     self.editButtonItem.title = @"編輯";    self.navigation.rightBarButtonItem = self.editButtonItem;    [self initTableViewData];	// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)initTableViewData{    NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];    dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [dataArr count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"tableCell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];        NSUInteger row = [indexPath row];    NSDictionary *rowDict = [dataArr objectAtIndex:row];    cell.textLabel.text =  [rowDict objectForKey:@"itemName"];    NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);        NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];    cell.imageView.image = [UIImage imageNamed:imagePath];    NSLog(@"cell.image.image  =  %@",imagePath);        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        return cell;}//選中Cell響應事件- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中后的反顯顏色即刻消失    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [dataArr objectAtIndex:row];    NSString *userName =  [rowDict objectForKey:@"itemName"];    NSLog(@"userName=%@",userName);}//返回編輯狀態的style- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    //UITableViewCellEditingStyleInsert//    return UITableViewCellEditingStyleNone;    return UITableViewCellEditingStyleDelete;}//完成編輯的觸發事件- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete)    {        [dataArr removeObjectAtIndex: indexPath.row];        //        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]        //                         withRowAnimation:UITableViewRowAnimationFade];        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]                         withRowAnimation:UITableViewRowAnimationFade];        [tableView reloadData];    } }//UIViewController生命周期方法,用于響應視圖編輯狀態變化- (void)setEditing:(BOOL)editing animated:(BOOL)animated {    [super setEditing:editing animated:animated];        [self.tableView setEditing:editing animated:YES];    if (self.editing) {     self.editButtonItem.title = @"完成";    } else {        self.editButtonItem.title = @"編輯";    }}@end

 2.移動單元格

 

Cpp代碼 復制代碼 收藏代碼
  1. //完成移動的觸發事件,不添加該方法不實現移動功能  
  2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath  
  3.       toIndexPath:(NSIndexPath *)destinationIndexPath  
  4. {  
  5.     NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];  
  6.     [dataArr removeObjectAtIndex:sourceIndexPath.row];  
  7.     [dataArr insertObject:item atIndex:destinationIndexPath.row];  
  8. }  
//完成移動的觸發事件,不添加該方法不實現移動功能- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath      toIndexPath:(NSIndexPath *)destinationIndexPath{    NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];    [dataArr removeObjectAtIndex:sourceIndexPath.row];    [dataArr insertObject:item atIndex:destinationIndexPath.row];}

 3.添加單元格。下面是自定義觸發事件,即單擊左下角的add按鈕

Cpp代碼 復制代碼 收藏代碼
  1. - (IBAction)addistItem:(UIBarButtonItem *)sender {  
  2.     AppUtils *appUtils = [AppUtils alloc];  
  3.     //需要先初始化一個UIAlertView  
  4.     UIAlertView *alert = [UIAlertView alloc];  
  5.     [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{  
  6.         //得到輸入框  
  7.         UITextField *textField=[alert textFieldAtIndex:0];  
  8. //        不要寫成NSMutableDictionary *newItem = [NSDictionary dictionary];  
  9.         NSMutableDictionary *newItem = [NSMutableDictionary dictionary];  
  10.         [newItem setObject:textField.text forKey:@"itemName"];  
  11.         [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];  
  12.         [dataArr addObject:newItem];  
  13.         [self.tableView reloadData];  
  14.     })];  
  15. }  
- (IBAction)addistItem:(UIBarButtonItem *)sender {    AppUtils *appUtils = [AppUtils alloc];    //需要先初始化一個UIAlertView    UIAlertView *alert = [UIAlertView alloc];    [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{        //得到輸入框        UITextField *textField=[alert textFieldAtIndex:0];//        不要寫成NSMutableDictionary *newItem = [NSDictionary dictionary];        NSMutableDictionary *newItem = [NSMutableDictionary dictionary];        [newItem setObject:textField.text forKey:@"itemName"];        [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];        [dataArr addObject:newItem];        [self.tableView reloadData];    })];}

 4.附上·AppUtils類

Cpp代碼 復制代碼 收藏代碼
  1. #import "AppUtils.h"  
  2. #include "RIButtonItem.h"  
  3. #include "UIAlertView+Blocks.h"  
  4.   
  5. @implementation AppUtils  
  6.   
  7. //彈出警告框,并實現警告框按鈕的觸發事件  
  8. - (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{  
  9.     RIButtonItem* cancelItem = [RIButtonItem item];  
  10.     cancelItem.label = @"No";  
  11.     cancelItem.action = ^  
  12.     {  
  13.         //為NO時的處理  
  14.         UITextField *tf=[alert textFieldAtIndex:0];  
  15.         NSLog(@"UITextField=%@",tf.text);  
  16.     };  
  17.       
  18.     RIButtonItem* confirmItem = [RIButtonItem item];  
  19.     confirmItem.label = @"Yes";  
  20. //    confirmItem.action = action;  
  21.     alert = [alert initWithTitle:title  
  22.                                                     message:message  
  23.                                            cancelButtonItem:cancelItem  
  24.                                            otherButtonItems:confirmItem, nil];  
  25.       
  26.     alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
  27.       
  28.     confirmItem.action = action;  
  29.     [alert show];  
  30. }  
  31. @end  
#import "AppUtils.h"#include "RIButtonItem.h"#include "UIAlertView+Blocks.h"@implementation AppUtils//彈出警告框,并實現警告框按鈕的觸發事件- (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{    RIButtonItem* cancelItem = [RIButtonItem item];    cancelItem.label = @"No";    cancelItem.action = ^    {        //為NO時的處理        UITextField *tf=[alert textFieldAtIndex:0];        NSLog(@"UITextField=%@",tf.text);    };        RIButtonItem* confirmItem = [RIButtonItem item];    confirmItem.label = @"Yes";//    confirmItem.action = action;    alert = [alert initWithTitle:title                                                    message:message                                           cancelButtonItem:cancelItem                                           otherButtonItems:confirmItem, nil];        alert.alertViewStyle = UIAlertViewStylePlainTextInput;        confirmItem.action = action;    [alert show];}@end

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品视频免费在线| 久久久国产成人精品| 国产精品久在线观看| 日韩在线视频国产| 奇米4444一区二区三区| 91禁国产网站| 国产成人精品视频在线观看| 国产欧美日韩视频| 久久影视三级福利片| 日韩动漫免费观看电视剧高清| 国产三级精品网站| 日韩av电影免费观看高清| 亚洲高清av在线| 亚洲欧美制服综合另类| 国产69精品久久久久99| 国产成人鲁鲁免费视频a| 欧美大肥婆大肥bbbbb| 92看片淫黄大片看国产片| 美日韩精品免费视频| 97香蕉超级碰碰久久免费的优势| 成人中文字幕+乱码+中文字幕| 美女久久久久久久久久久| 91中文在线视频| 啪一啪鲁一鲁2019在线视频| 51精品国产黑色丝袜高跟鞋| 91精品视频在线免费观看| 大量国产精品视频| 欧美乱人伦中文字幕在线| 国产精品久久99久久| 久久久久女教师免费一区| 欧美大片在线免费观看| 懂色aⅴ精品一区二区三区蜜月| 国产精品美女免费看| 日韩久久午夜影院| 欧美激情久久久久久| 国产中文字幕91| 亚洲欧美一区二区精品久久久| 欧美成人剧情片在线观看| 国产欧美精品一区二区| 日韩av成人在线观看| 91精品国产91久久久久久不卡| 欧美国产欧美亚洲国产日韩mv天天看完整| 日本午夜精品理论片a级appf发布| 国产精品99久久久久久久久久久久| 成人激情在线观看| 岛国av午夜精品| 日韩精品中文字幕在线观看| 91嫩草在线视频| 国产日韩在线亚洲字幕中文| 亚洲人在线观看| 日韩免费在线看| 欧美午夜无遮挡| 欧美成人精品一区二区三区| 91在线无精精品一区二区| 中文字幕日韩有码| 在线看福利67194| 久久欧美在线电影| 国产精品美女呻吟| 国产在线精品一区免费香蕉| 8090成年在线看片午夜| 日韩美女免费视频| 蜜臀久久99精品久久久无需会员| 国产专区精品视频| 久久人人爽亚洲精品天堂| 久久精品99久久久久久久久| 欧美国产亚洲精品久久久8v| 91超碰中文字幕久久精品| 久久久久久久久久久久久久久久久久av| 亚洲a在线观看| 精品伊人久久97| 国产精品久久久久久久久久免费| 欧美激情一区二区久久久| 91夜夜揉人人捏人人添红杏| 欧美成年人在线观看| 国产啪精品视频| 97久久超碰福利国产精品…| 国产精品黄页免费高清在线观看| 日韩中文视频免费在线观看| 午夜精品久久久久久久男人的天堂| 亚洲欧美自拍一区| 欧美一级大片在线免费观看| 性欧美暴力猛交69hd| 国产精品综合网站| 最近更新的2019中文字幕| 日韩av免费在线播放| 高清欧美性猛交xxxx黑人猛交| 亚洲欧美制服中文字幕| 欧美性极品少妇精品网站| 疯狂欧美牲乱大交777| 成人亲热视频网站| 最新国产精品亚洲| 亚洲网站在线看| 亚洲国产精品中文| 久久久久久久影视| 国产午夜精品全部视频播放| 88xx成人精品| 国产一区二区三区丝袜| 久久精品成人欧美大片古装| 91国内产香蕉| 欧美精品一区二区三区国产精品| 欧美日韩中文字幕在线| 亚洲区中文字幕| 欧美一区二区三区四区在线| 97国产suv精品一区二区62| 国产精品69久久| 中文字幕在线看视频国产欧美在线看完整| 成人免费视频在线观看超级碰| 久久久亚洲网站| 国产成人精品亚洲精品| 国产精品爽爽ⅴa在线观看| 91免费国产网站| 中文字幕亚洲天堂| 亚洲福利在线视频| 日韩网站免费观看高清| 欧美中文在线免费| 欧美综合激情网| 国产精品爽爽爽爽爽爽在线观看| 成人黄色在线免费| 国产精品久久久久久久app| 国产亚洲成精品久久| 国产成人综合久久| 热久久美女精品天天吊色| 久久免费观看视频| 一区二区在线视频播放| 俺去了亚洲欧美日韩| 国产精品入口夜色视频大尺度| 在线精品国产欧美| 久久九九全国免费精品观看| 中文字幕视频一区二区在线有码| 国模私拍视频一区| 97在线免费视频| 亚洲韩国日本中文字幕| 日日摸夜夜添一区| 在线看日韩欧美| 亚州欧美日韩中文视频| 91久久久久久国产精品| 欧美激情视频在线观看| 久久精品视频导航| 91成人天堂久久成人| 国产精品高潮呻吟久久av黑人| 日本道色综合久久影院| 国产精品青草久久久久福利99| 成人黄色激情网| xxx欧美精品| 岛国av一区二区在线在线观看| 精品久久久国产精品999| 亚洲人精品午夜在线观看| 精品久久久久久久久久久| 精品久久久久久| 久久久久久国产免费| 久久精品青青大伊人av| 中文字幕亚洲欧美| 欧美亚洲另类在线| 亚洲欧美变态国产另类| 欧美美最猛性xxxxxx| 91理论片午午论夜理片久久| 国产精品高潮呻吟久久av野狼| 欧美激情精品久久久久久久变态| 国产精品一二区| 成人在线播放av| 欧美极品少妇xxxxⅹ喷水| 欧美午夜视频在线观看| 亚洲色图激情小说| 日韩电视剧免费观看网站|