无法格式化UITableView的节标题

其中包含三个表视图的一个视图控制器。我正在努力格式化和设置每个表的节标题。 viewForHeaderInSection 的函数附在下面

我的目标是实现类似图像的效果。努力格式化和设计每个表视图的节标题。由于未捕获的异常“ CALayerInvalid”,使用附加的代码获取终止应用程序,原因:“层是其层树中循环的一部分” 错误消息

无法格式化UITableView的节标题

class Tabs extends StatefulWidget {
  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: GestureDetector(
            onTap: () {
              setState(() {
                tab1IsSelected = true;
              });
            },child: Container(
              decoration: BoxDecoration(
                color: tab1IsSelected ? primary : second,),child: Padding(
                padding:
                    EdgeInsets.only(top: 1.5 * SizeConfig.heightMultiplier),child: Center(
                  child: Text(
                    'New Hunt',style: Theme.of(context).textTheme.bodyText1,Expanded(
          child: GestureDetector(
            onTap: () {
              setState(() {
                tab1IsSelected = false;
              });
            },child: Container(
              decoration: BoxDecoration(
                color: tab1IsSelected ? second : primary,child: Center(
                  child: Text(
                    'My Hunts',style: Theme.of(context).textTheme.bodyText2,],);
  }
}


任何帮助和帮助将不胜感激。谢谢!

iCMS 回答:无法格式化UITableView的节标题

在您的viewForHeaderInSection函数中,您正在每个UIView块内创建一个view对象if 。在每个if块的末尾,那个对象超出了范围……它不再存在。

Xcode在此行不给您警告或错误:

return view

因为view是控制器的现有对象。

这是对创建的变量使用“内置”对象名称的一种不好的做法。

将函数的顶部更改为此:

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

    let view = UIView(frame: CGRect(x: 20,y: 8,width: tableView.frame.size.width,height: 30))

    if(tableView == firstTableView){
        // etc...

并从每个if块中删除该行。

更好的是,将其更改为:

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

    let myHeaderView = UIView(frame: CGRect(x: 20,height: 30))

    if(tableView == firstTableView){
        // etc... but change all instances of view. to myHeaderView.

    } // end of last if block

    return myHeaderView
 }
本文链接:https://www.f2er.com/1516472.html

大家都在问