UITableView 使用小结

本文介绍了UITableView的几个实用技巧,包括如何隐藏多余的分隔线、设置选中cell的背景颜色及解决选中状态复现的问题,并提供了一种方法来去除UITableView header的粘连效果。

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

1。UITableView 隐藏多余的分隔线

+(void)setExtraCellLineHidden:(UITableView*)tableView{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}

2。UITableView 定义选中cell的背景颜色

cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
cell.selectedBackgroundView.backgroundColor = TABLEVIEW_SELECTED_COLOR;

清除选中颜色就不用说了

今天做项目时突然发现取消选中状态后,tableView 复用时选中状态又出现了,原来的代码

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];

如果有这样的问题可以换uitableview 的方法

[tableView deselectRowAtIndexPath:indexPath animated:NO];


3.去掉UItableView header 粘连效果(网友写的,很好用)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == atWhoTableView)
    {
        CGFloat sectionHeaderHeight = 24; //sectionHeaderHeight
        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);
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值