接UITableView[1]我們對之前的視圖代碼進行編輯
#pragma mark -------表視圖編輯--------
//將表示圖處於可編輯狀態
- (void)setEdting:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:edting animated:animated];
[_tableView setEdting:editng animated:animated];
}
//指定表視圖哪些行可以進行編輯 默認是打開的
- (void)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//設置表示圖編輯樣式是刪除還是添加 默認是刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
// UITableViewCellEditingStyleNone,
// UITableViewCellEditingStyleDelete,
// UITableViewCellEditingStyleInsert
}
//編輯我弄成之后需要做什么事情,這才是我們編輯過程的核心代碼,項目開發中,增加,刪除的都在這里完成的
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath
{
[tableView beginUpdates];
// NSLog(@"你刪不掉的");
if (editingStyle == UITableViewCellEditingStyleDelete) {
//第一步 將選中的單元格刪除 //[indexpath]代表的這行的數據 是個數組
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//第二步 將刪除的單元格之前現實的內容從數組中移除 如果沒有 updates必須在第一刪數據
[_studentArray removeObjectAtIndex:indexPath.row];
}
else{
NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView insertRowsAtIndexPaths:@[indexPath1] withRowAnimation:UITableViewRowAnimationLeft];
Student *stu = [[Student alloc] init];
stu.name = @"張三";
stu.gender = @"男";
stu.age = @"22";
stu.picture = @"0";
[_studentArray insertObject:stu atIndex:0];
[stu release];
}
[tableView endUpdates];
}
#pragma mark -------表示圖編輯---------
//第一步同上 開啟編輯模式
//第二步 可移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:
(NSIndexPath *)indexPath
{
return YES;
}
//移動完需要做什么事,這才是移動的核心方法,之前的都是輔助的.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath
{
Student *stu = [_studentArray objectAtIndex:sourceIndexPath.row];
[_studentArray removeObjectAtIndex:sourceIndexPath.row];
[_studentArray insertObject:stu atIndex:destinationIndexPath.row];
//交換位置
// [_studentArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
UITableViewController繼承自UIViewController,自帶一個tableView
self.view 不是UIView而是UITableView
datasource和delegate默認都是self(UITableViewController)
開發中主要建立UITableViewController子類
顧名思義:一個類只有唯一的一個實例
系統的單例類有UIDevice,UIScreen,NSBundle,NSFileManager,NSUserDefaults等等.
單例類一般會提供一個類方法,獲得這個唯一的實例
總結:
無論編輯還是移動,都先讓tableView進⼊入編輯狀態。
編輯結束或者移動結束,要先修改數組和字典中的數據,在更改UI
UITableViewController是封裝好了各種delegate和datasource,能 提⾼高我們開發速度。
這個類方法常以default,share,main等單詞開頭,與便利構造器的命名規范是不同的,
我們自己在定義單例類的時候,一定要保證外界能獲得唯一的實例! 即每次獲得同一個對象.
一段新建了個Singleton類 繼承NSObject
//如何保證這個單例獲取的是同一個對象
+ (Singleton *)shareInstance
{
//同步鎖 解決多線程的情況時候出現問題,只有先創建一個才會創建另一個
//如果是在多線程里面,剛開始的時候,如果有倆個線程同時第一次調用這個方法,會產生倆個線程創建了倆個對象,為了避免這種情況的發生,我們需要做如下修改:
@synchronized(self){ //添加了一個同步鎖,同步鎖的作用就是一次只能有一個線程在訪問{}內部的內容,如果有多個內容的話,那么其他線程就處於等待狀態.
if(s == nil){
s = [[Singleton alloc] init];
// s = [[super allocWithZone:nil] init];
}
}
return s;
}
self.tableView.tableFooterView = [[UIView alloc] init];
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。