`

[IOS]如何自定义UITableView的section样式

    博客分类:
  • IOS
阅读更多

一.如何配置section

参考:https://www.youtube.com/watch?v=-yeaLC0jgss

配置无标题section:

let sections = [" "," "]

 不能直接"",IDE会默认为没有section

配置DataSource:

menuList = [
            [menuPingTest,menuPortForword,menuDhcpRes],
            [menuDataTraffic]
        ]

 配置表:

func initView() {
        advancedTable.dataSource = self
        advancedTable.delegate = self
        advancedTable.backgroundColor = uiStyleUtil.getBackGroundGrayColor()
        advancedTable.tableFooterView = UIView.init(frame: CGRect.zero)  //不显示无数据的空行
        advancedTable.separatorColor = uiStyleUtil.getTableLineGrayColor()
    }

 section基本:

//Section
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sections.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sections[section]
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }

 table cell基本:

//table view
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuList[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "AdvancedSubCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        
        cell?.textLabel?.text = menuList[indexPath.section][indexPath.row]
        cell?.detailTextLabel?.text = ""
        cell?.backgroundColor = UIColor.clear
        cell?.selectionStyle = UITableViewCell.SelectionStyle.none
        return cell!
    }

 

二.自定义section相关:

1.如果section行跟随table滚动,在storyboard table属性那里选择grouped

但是这里有个问题就是选择grouped以后,section的样式就会和cell一样,配置背景色等等是没有效果的

section的separator line样式不能设置,默认就是全屏宽(即使cell设置了separator insert)

 

2.不选择grouped,选择plain可以自定义:

有两种方式设置section的样式:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)

 这种可以做简单的自定义,例如背景色,字体颜色等等

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = uiStyleUtil.getBackGroundGrayColor()
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.textColor = UIColor.white
        separatorView.backgroundColor = uiStyleUtil.getTableLineGrayColor()
        header.addSubview(separatorView)
    }

 但是这种方式不能有针对性地设置separator line:

所以有另外一种定义:

参考:https://stackoverflow.com/questions/45139262/tableview-section-seperator-line

 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

 

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?

 这种和第一种会冲突,只能选择一种.这种是把section给一个header view和footer view,把他们给不同背景色就可以看出来,如果两个都配置了,会发现section的高度会变大,因为多了一个view

那如果想给section加上上下分割线怎么做呢,我们只要一个view,我的例子是:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        let separatorHeaderView = UIView(frame: CGRect(x: tableView.separatorInset.left,
                                                       y: headerView.frame.height,
                                                       width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left,
                                                       height: 1))
        let separatorFooterView = UIView(frame: CGRect(x: tableView.separatorInset.left,
                                                       y: headerView.frame.height + 20,
                                                       width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left,
                                                       height: 1))
        separatorHeaderView.backgroundColor = uiStyleUtil.getTableLineGrayColor()
        separatorFooterView.backgroundColor = uiStyleUtil.getTableLineGrayColor()
        if section == 1 {
            headerView.addSubview(separatorHeaderView)
        }
        headerView.addSubview(separatorFooterView)
        print("header view: frame-h: \(headerView.frame.height) bounds-h: \(headerView.bounds.height)")
        return headerView
    }

 这样,样式就是



 

  • 大小: 20.4 KB
分享到:
评论

相关推荐

    IOS代码中使用自定义UITableView

    自定义UITableView还包括对行间的间距、分割线样式、背景颜色等视觉元素的调整。这些可以通过UITableView的属性设置,如`separatorStyle`、`tableFooterView`、`backgroundColor`等。 此外,对于复杂的表格结构,...

    设置UITableView Section的背景颜色和字体颜色

    在某些情况下,开发者可能希望自定义UITableView的外观,比如改变Section的背景颜色和字体颜色,以满足特定的设计需求。以下是如何实现这一功能的详细步骤。 首先,我们来了解UITableView的Section。Section是...

    ios-UITableView右侧索引.zip

    4. 自定义样式:默认的索引样式可能不满足所有设计需求,你可以通过自定义`UITableViewIndex`来改变颜色、字体等样式。 接下来,我们讨论分组头部。分组头部是在每个数据分组顶部的视图,用于标识该组的内容。在...

    IOS UITableView 的简单案例

    在iOS开发中,UITableView是一种非常重要的控件,用于展示列表数据。这个简单的案例将向我们展示如何使用UITableView来...实践过程中,你可以根据实际需求对单元格进行自定义,例如添加图片、使用不同的单元格样式等。

    仿IOS式ListVIew UITableView

    在Android开发中,为了实现与iOS应用类似的列表展示效果,开发者常常会仿照iOS的UITableView来创建自定义的ListView。这个“仿IOS式ListVIew UITableView”项目正是一个旨在为Android平台提供类似功能的实现。以下...

    ios自定义table例子

    本示例“ios自定义table例子”旨在教你如何通过自定义UITableViewCell来实现个性化展示,让你能够在表格中自由地绘制内容。 首先,自定义一个UITableViewCell类。在Swift中,你可以创建一个新的Swift文件,例如`...

    Xamarin.iOS-UITableView详细使用说明

    Xamarin.iOS提供了默认的样式,如Default和Subtitle,也可以创建自定义单元格类。在`tableView(_:cellForRowAt:)`中初始化并配置单元格,设置文本标签、图像等。 4. 注册单元格: 在表格视图初始化时,需要注册...

    IOS UITableView及索引条源码

    在iOS开发中,UITableView是...总结起来,理解并熟练掌握UITableView的使用,包括数据源、索引条、自定义Cell和响应用户操作等,是iOS开发中的重要技能。通过实践和不断优化,我们可以创建出流畅、功能丰富的用户界面。

    iOS-UITableview 的cell边线阴影

    在iOS开发中,`UITableView`是用于展示列表数据的核心组件,而给`UITableViewCell`添加边线阴影以及渐变效果能够提升界面的视觉层次感和用户体验。这篇内容将深入讲解如何实现`UITableView`的cell边线阴影及渐变设计...

    ios实现UITableView之间圆角和间隙

    在iOS中,`UITableView`的分割线默认是垂直显示在每个单元格之间,可以通过设置`separatorStyle`属性来隐藏或改变样式。在代码中,我们使用`UITableViewCellSeparatorStyleNone`将分割线设置为不可见: ```swift ...

    安卓Android源码——UITableView ios风格控件.rar

    4. **Section Indexing**:iOS的UITableView可以有分段索引,允许快速跳转到特定部分。在Android中,可能需要自定义实现这种功能。 5. **滑动删除/编辑**:iOS的UITableView支持滑动手势来删除或编辑单元格,这个...

    iOS点击section头展开再次点击合起

    在iOS应用开发中,用户界面的设计与交互是关键部分,其中一种常见的功能是实现UITableView的Section头点击展开和合起。这个功能允许用户通过点击Section头部来显示或隐藏该Section下的内容,提供了一种交互式的浏览...

    swift中UITableView自定义cell添加与删除及表的刷新wift中UITableView自定义cell添加与删除及表的刷新

    以上就是在Swift中自定义UITableView的Cell,以及处理数据的添加、删除和刷新的基本操作。这只是一个基础的指南,实际项目中可能需要考虑更多细节,如动画效果、异步数据加载、错误处理等。通过不断地实践和学习,你...

    MJ-iOS-UITableView:学习UITableView

    我们可以自定义它的样式,如默认样式(UITableViewCellStyleDefault)、子标题样式(UITableViewCellStyleSubtitle)等,或者完全自定义布局。在`tableView:cellForRowAtIndexPath:`中,我们可以通过`...

    iOS UITableView 树型

    然而,实际应用可能还需要处理更多细节,如递归加载数据、优化性能(避免一次性加载所有子节点)、以及自定义Cell的样式等。在"UITableView-Tree"的压缩包中,应该包含了完整的示例代码,你可以参考这些代码加深理解...

    iphone应用开发:UITableView的详细讲解(一)

    默认情况下,UITableView提供了一些基本的Cell样式,但为了实现更具吸引力和功能性的界面,通常需要自定义Cell。这可以通过创建一个新的UITableViewCell子类并在XIB或Storyboard中设计布局来完成。例如,你可以添加...

    iOS-使用表格视图UITableView

    总结来说,"iOS-使用表格视图UITableView"项目涵盖了iOS开发中的基础列表展示技术,通过学习和实践,开发者可以掌握如何有效地利用UITableView展示和管理数据,以及如何通过自定义单元格和交互来提升用户体验。...

    ios-自定义headerview,自适应header的高度.zip

    在iOS开发中,自定义`header view`并使其高度自适应是一种常见的需求,尤其是在实现诸如TableView或者CollectionView等滚动视图时。本项目“ios-自定义headerview,自适应header的高度.zip”着重探讨了如何优雅地...

    UITableView ios风格控件.rar

    UITableView还允许添加section header和footer,它们可以是静态文本或自定义视图。通过实现`tableView(_:viewForHeaderInSection:)`和`tableView(_:viewForFooterInSection:)`方法来定制。 七、reloadData()和插入/...

    iOS应用开发中UITableView的分割线的一些设置技巧

    本文将深入探讨UITableView的分割线设置技巧,包括如何消除分割线、自定义分割线的位置以及对sectionTitle进行个性化设置。 1. **消除分割线** 在iOS 7及更高版本中,由于UITableView内部使用了margin layout,...

Global site tag (gtag.js) - Google Analytics