【问题标题】:Customize MoreNavigationController's Cell自定义 MoreNavigationController 的 Cell
【发布时间】:2015-12-24 14:34:54
【问题描述】:

我正在尝试使更多视图与我的应用主题相匹配

表格单元格没有改变背景颜色。我已经尝试了几乎所有的答案和互联网上的博客。

tabBarController?.moreNavigationController.topViewController?.view.backgroundColor = UIColor.blackColor()

我已经使用上面的代码实现了如图所示的当前场景。

override func viewDidLoad() {
    self.homeTableView.registerNib(UINib(nibName: "Banner", bundle: nil), forCellReuseIdentifier: "Banner")
    self.homeTableView.registerNib(UINib(nibName: "Heading", bundle: nil), forCellReuseIdentifier: "Heading")
    self.homeTableView.registerNib(UINib(nibName: "ThumblessList", bundle: nil), forCellReuseIdentifier: "ThumblessList")
    self.homeTableView.registerNib(UINib(nibName: "MapCell", bundle: nil), forCellReuseIdentifier: "MapCell")
    self.homeTableView.registerNib(UINib(nibName: "CallButton", bundle: nil), forCellReuseIdentifier: "CallButton")
    self.searchBar.showsCancelButton = true
    self.edgesForExtendedLayout = UIRectEdge.None
    self.extendedLayoutIncludesOpaqueBars = false
    self.automaticallyAdjustsScrollViewInsets = false
    tabBarController?.moreNavigationController.topViewController?.view.backgroundColor = UIColor.blackColor()
}

【问题讨论】:

  • 有什么问题?你希望你的细胞长什么样?还可以考虑包含更多代码。至少您的 Table View 数据源方法。
  • 感谢您的回复。我没有为此配置数据源。我已经将上面的代码放在我的第一个 View 控制器的 viewDidLoad() 中。单元格需要有黑色背景。
  • 这些静态单元格是您使用 xibs 配置的吗?
  • 这些是我在标签栏中添加超过 5 个项目时自动生成的单元格

标签: ios swift uitabbarcontroller


【解决方案1】:

我最终创建了一个新的视图控制器并将其添加为 iPhone 的第 5 个选项卡项。对于 Ipad,我创建了一个单独的故事板,没有新的视图控制器。

谢谢 :) 斯沃鲁普

【讨论】:

    【解决方案2】:

    这是一个非常晚的答案,但我无法在任何地方在线找到解决此问题的单一解决方案,即使设置表格视图的颜色和 UITabBarController 的 moreNavaigationController 中的单元格应该是您可以在故事板。我觉得这是 Swift/xcode 开发人员的疏忽。

    也就是说,在您的UITabBarControllerviewDidAppear 方法中:

    override func viewDidAppear(_ animated: Bool) {
    
        if let moreTableView = moreNavigationController.topViewController?.view as? UITableView {
    
            moreTableView.backgroundColor = UIColor.YOURCOLOUR
    
            for cell in moreTableView.visibleCells {
                cell.backgroundColor = UIColor.YOURCOLOUR
            }
        }
    }
    

    如果您在viewDidLoad 方法中执行此代码,则此代码将不起作用。我希望这段代码可以帮助那些正在努力寻找解决方案的人!

    【讨论】:

    • 这可行,但是当您进入横向并且有项目离开屏幕时,它们会变回白色背景。这种情况有什么解决办法吗?
    • 它是如何工作的?我正在使用 xamarin 表单,但无法实现。
    • 如前所述,这不适用于表格视图中尚不可见的单元格。
    【解决方案3】:

    我遇到了同样的问题,想分享我解决这个问题的方法。也许对其他人有帮助...

    1. 为更多的 UITableView 数据源创建一个 holder:

      @property (nonatomic, weak, nullable) id moreDataSource;

    2. 在代码中的某处执行以下操作:

      if([tabBarController.moreNavigationController.topViewController.view isKindOfClass:UITableView.class]){

       UITableView *moreTable = (UITableView*)moreNavController.topViewController.view;
       self.moreDataSource = moreTable.dataSource;
       moreTable.dataSource = self;
      

      }

    3. UITableViewDataSource的实现方法:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.moreDataSource tableView:tableView numberOfRowsInSection:section]; }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewCell * cell = [self.moreDataSource tableView:tableView cellForRowAtIndexPath:indexPath];    
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      相关资源
      最近更新 更多