tableView的一些用法(持续更新)

本文汇总了UITableView的多种实用技巧,包括清除选中cell痕迹、指定位置刷新、header悬停、设置section间距、去除多余分割线、修改分割线颜色等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、 继承ViewController 时,选中的cell 跳转页面后返回还是出现刚才选中的选中色,我们可以通过以下代码消除cell选择痕迹:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	//跳转页面的代码

	//消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}

- (void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

2、指定的位置刷新。通常,我们用reloadData 刷新tableview,但是,有的时候有多个section,只想刷新指定的section 或者指定的cell ,则可以用以下代码实现:

//一个section刷新    

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];    

[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];    

//一个cell刷新    
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];    

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; 

原文链接http://leopard168.blog.163.com/blog/static/16847184420148229221893/


3、tableView header悬停的方法:

把 UITableView 的 style 属性设置为 Plain ,这个tableview的section header在滚动时会默认悬停在界面顶端。

如果想取消悬停效果,可以采用如下2种方法

  • 修改 UITableView 的 style 属性设置为 Grouped. 这时所有的section header都会随着scrollview滚动了。不过 grouped 和 plain 的样式有轻微区别,切换样式后也许需要重新调整UI。
  • 如果需要使用 Grouped 这种样式, 也可以通过重载 scrollView 的 delegate 来达到目的:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y > =0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

原文链接:http://www.jianshu.com/p/2bb137b15435

我尝试了第二种,使用grouped的样式,重写了- (void)scrollViewDidScroll:(UIScrollView *)scrollView;方法,但是没法实现,最终我在上一个section添加了一个footer,给了灰色的UIView,一样达到group的效果,有哪位朋友实现了grouped样式,实现了悬停的效果,麻烦教我一下。


4、设置group类型tableview的section间距

【转】http://blog.youkuaiyun.com/rbyyyblog/article/details/20062557?utm_source=tuicool&utm_medium=referral
在ios7中使用group类型的tableview时,第一个section距离navigationbar的距离很大,不符合这边的设计图。使用 myTableView . sectionHeaderHeight = 8.0无效。 于是通过各种方法测试,终于得到解决方法。就是通过设置tableview的headerview高度来控制这个距离。使用的方法是:
完全删除则return 一个大于零的数,比如0.1。

- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section

{

    return 8.0 ;

}

但对于第一个和第二个section之间的距离设置则不能使用- ( float )tableView:( UITableView *)tableView heightForFooterInSection:( NSInteger )section这个方法。需要使用

myTableView . sectionFooterHeight = 1.0。

这个距离的计算是header的高度加上footer的高度。


5、 去除多余的分割线

self.tableView.tableFooterView=[[UIView alloc]init];

6、修改tableView 分割线的颜色

[tableview  setSeparatorColor:[UIColor blueColor]];  //设置分割线为蓝色

7、tableViewCell 分割线的延长或缩短

系统自带的cell分割线有时不满足我们的需求,或增长,或剪短,这时候我们可以通过设置分割线的偏移来达到我们想要的效果。

// 将分割线 增长
_myTableView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 0);

8、去掉分割线

自带是有分割线的,当我们不想要分割线时,应该怎么操作呢?

_myTableView.separatorStyle = UITableViewCellSelectionStyleNone;    // 去除掉分割线

9、自定义cell的点击背景色

> 通过 selectedBackgroundView 给 cell 一个自定义点击之后看到的View
// 设置 cell 点击时的背景颜色
    UIView *view_bg = [[UIView alloc]initWithFrame:cell.frame];
    
    view_bg.backgroundColor = [UIColor colorWithRed:178/255.0 green:34/255.0 blue:34/255.0 alpha:0.7];
    // 将设置的 选择的背景 为UIVIEW
    cell.selectedBackgroundView = view_bg;

2017.03.04修改


10、滚动到指定位置

// 滚动到指定位置  
 [self.tableView reloadData];  
 
 NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:2];  
 
[self.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

2017.07.08修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值