數據刷新的原則
-
先獲取數據,修改數據之后修改展示
- 先獲取數據
- 調用數據刷新方法
不要直接修改cell上面的子控件的屬性
重新刷新屏幕上的所有cell
[self.tableView reloadData];
-
增加
- 增加必須先修改數據源,增加了相應的數據之后,增加的條數和insertRowsAtIndexPaths行號集合必須數量必須一致
// 插入某些特定的行 // insertRowsAtIndexPaths行號集合 [self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationLeft];
-
刪除
- 刪除數據,需要先將數據源中的條數刪除,刪除的條數和insertRowsAtIndexPaths行號集合必須數量必須一致
//刪除特定的行 [self.tableView deleteRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationRight];
-
修改
- 修改數據源中對應的模型
// 刷新特定的行 [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
刪除和增加的時候刷新表格,有的時候會出問題,最簡單的方法就是直接使用全部刷新
刪除功能還有左滑刪除
// 進入編輯模式
//self.tableView.editing = YES;
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
/**
* 只要實現這個方法,左劃cell出現刪除按鈕的功能就有了
* 用戶提交了添加(點擊了添加按鈕)\刪除(點擊了刪除按鈕)操作時會調用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 點擊了“刪除”
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// 點擊了+
}
}
/**
* 這個方法決定了編輯模式時,每一行的編輯類型:insert(+按鈕)、delete(-按鈕)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
* UITableViewCellEditingStyleNone,
* UITableViewCellEditingStyleDelete,
* UITableViewCellEditingStyleInsert
*/
return UITableViewCellEditingStyleDelete;
}