ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iOS表分区头和表视图的编辑模式

2019-09-09 23:04:36  阅读:360  来源: 互联网

标签:indexPath return NSIndexPath tableView self iOS 视图 UITableView 头和表


原文链接:https://my.oschina.net/zyboy/blog/617427

设置分区头信息
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section

设置分区头视图
-(UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section

表视图分区头到顶部时仍然存在,
TableView的style应该为Plain
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
return @“分区头”;
}

设置表头视图
UIImageView *headerImageView = [[UIImageView alloc]init];
headerImageView.frame = CGRectMake(0, 0, 0, 0);
headerImageView.image = [UIImage imageNamed:@“xxx.jpg”];
self.tableView.tableHeaderView = headerImageView;

表视图的编辑模式需要配置导航栏右侧的编辑按钮
可以使用系统版的editButtonItem来配置导航栏的右侧按钮,但文字是英文版
self.navigationItem.rightBarButtonItem = self.editButtonItem;
还可以自定义
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@”编辑” style:UIBarButtonItemStylePlain target:self action:@selector(clickEditButton:)];
点击编辑按钮,切换表格的编辑模式
-(void)clickEditButton:(UIBarButtonItem *)item
{
[self.tableView setEditing:!self.tableView.editing animated:YES];

item.title = self.tableView.editing?@"完成":@"编辑";

}

表格的编辑功能
问1:当前行是否可编辑
-(BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == 0) {
return NO;
}else{
return YES;
}
}
问2:当前行的编辑样式是什么
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == self.allCitys.count-1)
{
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
//return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;//可以多选
}
//答1:点击编辑按钮后做什么
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){//点击的是删除按钮
// 1.先删除行所在的数据模型中的数据
[self.allCitys removeObjectAtIndex:indexPath.row];

    // 2.更新tableView的显示
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}else{//点击的是增加按钮

    // 1.先将数据增加到数据模型中
    City *city = [[City alloc]init];
    city.name = @"test";
    city.population = 1000;
    [self.allCitys addObject:city];

    // 2.刷新表格
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.allCitys.count-1 inSection:0];
    [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

}
行的移动

//问1:改行是否可以移动
-(BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath
{
return YES;
}

//答1:移动后做什么
-(void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//1.按照旧的位置,找到原始的数据
City *city = self.allCitys[sourceIndexPath.row];

//2.将数据从原位置上移除
[self.allCitys removeObjectAtIndex:sourceIndexPath.row];

//3.再将数据按照新的位置插入回数组
[self.allCitys insertObject:city atIndex:destinationIndexPath.row];

}

转载于:https://my.oschina.net/zyboy/blog/617427

标签:indexPath,return,NSIndexPath,tableView,self,iOS,视图,UITableView,头和表
来源: https://blog.csdn.net/chunbo4007/article/details/100677713

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有