1.tableview修改和刷新数据的方法
数据的修改应该修改模型中的数据,对于tableview只需要刷新数据即可
全局刷新:
[self.tableview reloadData]; //这个方法会重载全部的cell的数据
局部刷新:
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; //这个方法会根据传递的数组中的行数信息,刷新指定的行数,但这个方法的使用前提是保证数组个数不变,,如果有数据的增加或删除则要使用下面的方法
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; //这个方法会根据传递的数组中的行数信息,增加行数到指定位置
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom]; //这个方法会根据传递的数组中的行数信息,删除指定的行数
2.设置左滑删除或其它功能
单个按钮(以删除功能为例):
/** 只要实现这个方法,就会提供系统默认的左滑删除按钮功能 */
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.phoneModels removeObjectAtIndex:indexPath.row];
[self.phoneTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
/** 这个方法可以修改系统提供的默认左滑按钮的文字 */
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @“删除”;
}
多个按钮:
/** 这个方法可以添加并自定义需要的左滑按钮,设置这个方法则以上两个方法会失效 */
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@“取消” handler:^
(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath){
self.phoneTableView.editing = NO; //退出编辑模式
}];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@“删除” handler:^
(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath){
[self.phoneModels removeObjectAtIndex:indexPath.row];
[self.phoneTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}];
return @[deleteAction];
}
3.UITableView的编辑模式
/** 设置一个按钮方法,点击后即可切换tableView的编辑模式 */
-(IBAction)editingMode:(id)sender
{
//self.phoneTableView.editing = !self.phoneTableView.isEditing;
[self.phoneTableView setEditing:!self.phoneTableView.isEditing animated:YES];
}