ios11使用tableView的一些问题

来源:互联网 发布:matlab中粒子群算法 编辑:程序博客网 时间:2024/05/20 17:41

随着ios11,xcode9的到了,一些新旧API的更替,以往tableView上可以正常显示的界面出现了一些问题,在这里简单记录一下


首先,如果在项目的tableView中,使用了
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath;
方法的话,那么在ios11下显示就会发现设置的高度全不见了,并且你还会发现,以往的各个section之间的间距,到这里也没有了
解决方法:
苹果在ios11中,对tableView新增了许多属性,其中有几条就是关于cell高度以及头尾视图高度的
只需要判断好系统版本,在初始化tableView时加上一下代码就可以了

if (iOS11Later)        {            _tableView.estimatedRowHeight = 0;            _tableView.estimatedSectionHeaderHeight = 0;            _tableView.estimatedSectionFooterHeight = 0;        }

如果你的cell没有固定行高,就不需要加_tableView.estimatedRowHeight = 0;
否则当你出现indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1之类的判断时,将会出现问题


cell侧滑删除的问题
ios11引进了两个新的cell侧滑的代理方法

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

第一个控制cell右滑出现的按钮,第二个控制左滑删除,置顶等按钮
这里举一个左滑删除的例子

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {    //删除    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {       //这里删除按钮点击或滑到最左侧时会触发这个回调       completionHandler (YES); //这里回调YES是执行删除操作    }];    //下面的属性可以自定义删除的样式    deleteRowAction.image = [UIImage imageNamed:@"address_cell_delete"];    deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"f4f4f4" alpha:1];    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];    return config;}

这里两个枚举值
UIContextualActionStyleNormal,//默认的滑动
UIContextualActionStyleDestructive//当滑动到最左侧时,会直接触发删除操作

注:
这里ios11的方法总会提示一些警告,一些提示让你对ios11进行版本判断,本人认为这些多此一举,只有在ios11时才会进入此方法中,无需一些多余的判断;
还有一个比较有意思的问题是,在自定义滑动删除样式的时候,通过deleteRowAction.image = [UIImage imageNamed:@”address_cell_delete”];给设置图片的时候,无论怎么去渲染,更改图片的颜色,在这里都会展示成白色的图片,目前还没有找到方法去解决,有解决的朋友欢迎替小弟解惑.

原创粉丝点击