日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发日记1-tableview编辑

發布時間:2025/7/14 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发日记1-tableview编辑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天博主有一個tableview編輯的需求,遇到了一些困難點,在此和大家分享,能夠共同進步.

tableview的編輯是通過[self.tableview setEditing: BOOL1 animotion: BOOL2];進入的,如果需要進入編輯模式,則調用方法,將BOOL1改為YES.如果要退出編輯模式,則調用方法,將BOOL1改為NO.BOOL2為是否使用動畫.

如果你想將編輯這個button由文字改為圖片,想通過setBackGroudImage這個方法替換文字,你會發現圖片會因為無法設置frame而產生拉伸,效果不理想.解決方法:你可以自定義rightBarButtonItem,重寫代理方法.下面將tableview的編輯的代碼貼出來,與大家分享

- (void)viewDidLoad {

? ? [super viewDidLoad];

?

? ? //編輯按鈕

? ? self.navigationItem.rightBarButtonItem = self.editButtonItem;

? ? self.navigationItem.rightBarButtonItem.title = @"編輯";

? ? self.navigationItem.rightBarButtonItem.tintColor=[UIColor blackColor];

}

#pragma mark---------tableView的編輯(刪除,插入)

//點擊編輯按鈕

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

? ? [super setEditing:editing animated:animated];

? ? // Don't show the Back button while editing.

//? ? [self.navigationItem setHidesBackButton:editing animated:YES];

? ? if (editing) {

? ? ? ? self.navigationItem.rightBarButtonItem.title = @"完成";

? ? ? ? NSLog(@"abc");

? ? }else {//點擊完成按鈕

? ? ? ? self.navigationItem.rightBarButtonItem.title = @"編輯";

? ? ? ? NSLog(@"123");

? ? }

? ? [_customView.tableView setEditing:editing animated:YES];

}

//2.設置定制分區(section)中的行(row)是否可以被編輯

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

//? ? if (indexPath.section==0) {

//? ? ? ? return NO;

//? ? }

? ? return YES;

}

//3.設置指定的分區(section)的行(row)編輯的樣式(添加,刪除)

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

{

? ? if (indexPath.section==1) {

? ? ? ? return UITableViewCellEditingStyleInsert;

? ? }

? ? return UITableViewCellEditingStyleDelete;

}

//4.編輯完成(先操作數據源,再修改UI)

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

{

? ? if (editingStyle==UITableViewCellEditingStyleDelete) {

? ? ? ? NSLog(@"刪除");

? ? ? ? //同步更新beginUpdates 和 endUpdates 之間修改的UI和數據

? ? ? ? [_customView.tableView beginUpdates];

?? ? ? ?

? ? ? ? //1.刪除數據

? ? ? ? NSString *key=_allDataDic.allKeys[indexPath.section];

? ? ? ? NSMutableArray *array=_allDataDic[key];

? ? ? ? [array removeObjectAtIndex:indexPath.row];

? ? ? ? //2.刪除UI

? ? ? ? //刪除行

? ? ? ? [_customView.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

? ? ? ? if (array.count==0) {

?? ? ? ? ? ?

? ? ? ? ? ? //刪除區頭上的數據(刪除key對應的小數組)

? ? ? ? ? ? NSString *key=_allDataDic.allKeys[indexPath.section];

? ? ? ? ? ? [_allDataDic removeObjectForKey:key];

?

? ? ? ? ? ? //當某個分區中只剩一個cell的時候(或者小數組中只剩一個數據的時候),刪除時,也要把區頭刪除掉

? ? ? ? ? ? NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:indexPath.section];

? ? ? ? ? ? [_customView.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationLeft];

? ? ? ? }

?? ? ? ?

? ? ? ? [_customView.tableView endUpdates];

?? ? ? ?

? ? }else if (editingStyle==UITableViewCellEditingStyleInsert)

? ? {

? ? ? ? NSLog(@"插入");

? ? ? ? //1.插入數據

? ? ? ? NSString *key=_allDataDic.allKeys[indexPath.section];

? ? ? ? NSMutableArray *array=_allDataDic[key];

? ? ? ? [array insertObject:@"昌平" atIndex:indexPath.row];

? ? ? ? //2.插入UI(cell)? ? ? ??

? ? ? //? NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:0 inSection:indexPath.section];//保證每次插入數據都在第一行

? ? ? ? [_customView.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];? ? ? ??

? ? }

}

?

http://www.jianshu.com/p/7c3d72fd9616?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1

轉載于:https://www.cnblogs.com/Twisted-Fate/p/4727766.html

總結

以上是生活随笔為你收集整理的iOS开发日记1-tableview编辑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。