iOS UITableView划动删除的实现
標簽:劃動刪除iphone滑動刪除iosUITableView
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章原始出處、作者信息和本聲明。否則將追究法律責任。http://rainbird.blog.51cto.com/211214/634587
從七八月前對蘋果一無所知,到現在手持iphone,ipad,itouch有三個線上成熟app并熟練開發ios應用.一路走來一直站在前輩的肩膀上不斷進步.如今生活工作穩定是時候將一直以來的一些心得整理出來了.想來想去決定先說說UITableView.
對于app應用來說,使用列表的形式展現數據非UITableView莫屬.在熟練掌握了用UITableView展示數據以后,是不是也遇到了需要刪除數據的需求?是不是覺得在一行數據上劃動一下,然后出現一個刪除按鈕很酷?廢話少說,直奔正題,就由筆者來向您展示一下這個功能的實現是多么容易.
先前的準備工作:
第一步,準備好數據源.
#import<UIKit/UIKit.h>
@interfaceUITableCellSwapDeleteViewController:UIViewController<UITableViewDelegate>{
IBOutletUITableView*testTableView;
NSMutableArray*dataArray;
}
@property(nonatomic,retain)UITableView*testTableView;
@property(nonatomic,retain)NSMutableArray*dataArray;
@end
-(void)viewDidLoad{
[superviewDidLoad];
dataArray=[[NSMutableArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
}
這里筆者定義了并實現了一個一維的可變數組.為什么要用可變數組呢?因為我們要刪除里面的數據呀.
第二步,展示數據.
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
//Returnthenumberofsections.
return1;
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
//Returnthenumberofrowsinthesection.
return[dataArraycount];
}
//Customizetheappearanceoftableviewcells.
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString*CellIdentifier=@"Cell";
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier]autorelease];
}
//Configurethecell...
cell.textLabel.text=[dataArrayobjectAtIndex:indexPath.row];
returncell;
}
通過實現上面三個代理方法向UITableView中添加了數據.
通過上面兩步就實現了數據展示工作,接下就實現關鍵的數據刪除了.
-(BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath{
returnYES;
}
-(void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath{
if(editingStyle==UITableViewCellEditingStyleDelete){
[dataArrayremoveObjectAtIndex:indexPath.row];
//Deletetherowfromthedatasource.
[testTableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
elseif(editingStyle==UITableViewCellEditingStyleInsert){
//Createanewinstanceoftheappropriateclass,insertitintothearray,andaddanewrowtothetableview.
}
}
啟用上面兩個代理,并增加數據刪除操作:
[dataArray removeObjectAtIndex:indexPath.row];
在一條數據上向右劃動一下.
點Delete.
是不是就成功刪除了一條數據呢?
按理說故事講到這里也就講完了.但是筆者想延伸一下.注意看圖二劃動以后的"Delete",你有沒有想把這個東東改掉的沖動呢?比如改成:下載?其實很簡單,其實下面這個代理方法:
-(NSString*)tableView:(UITableView*)tableViewtitleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
return@"下載";
}
再劃動一下,是不是變了呢?
相關文章:
UITableView多選刪除,類似mail中的多選刪除效果
具體代碼見附件
本文出自 “rainbird” 博客,請務必保留此出處http://rainbird.blog.51cto.com/211214/634587
總結
以上是生活随笔為你收集整理的iOS UITableView划动删除的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gateway-workman
- 下一篇: Windows 文件夹文件含义说明