編輯模式
- 移除
獲得delete按鈕控制
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%d",indexPath.row);
}
- 移除指定物件並回寫我的最愛
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%d",indexPath.row);
[tbView beginUpdates];
//移除畫面上某筆資料
[tbView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//移除陣列中資料,否則資料與畫面對不上會產生錯誤
[items removeObjectAtIndex:indexPath.row];
//更新畫面
[tbView endUpdates];
[[NSUserDefaults standardUserDefaults] setObject:items forKey:@"FAVORITE"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath ==0){
return NO;
}else{
return YES;
}
}
調整順序
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//要移除前先指定給暫存
NSDictionary *entry = items[sourceIndexPath.row];
//移除這個位置原本的item
[items removeObjectAtIndex:sourceIndexPath.row];
//將暫存插入原本位置
[items insertObject:entry atIndex:destinationIndexPath.row];
//更新我的最愛
[[NSUserDefaults standardUserDefaults] setObject:items forKey:@"FAVORITE"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
留言
張貼留言