Ian*_*971 20 iphone uitabbarcontroller ios
我在我的应用程序上使用标签栏(UITabBarController),我希望自定义单击更多按钮时出现的表格的外观.我已经设定了如何通过设置更改更多屏幕上的导航栏的外观
self.moreNavigationController.navigationBar.barStyle
Run Code Online (Sandbox Code Playgroud)
在UITabBarController的子类中,我已经设法通过修改来更改表的背景颜色
self.moreNavigationController.topViewController.view.backgroundColor
Run Code Online (Sandbox Code Playgroud)
,但我无法弄清楚如何更改表格中出现的单元格中的字体颜色.我希望我能用
self.moreNavigationController.topViewController.view.visibleCells
Run Code Online (Sandbox Code Playgroud)
但这似乎总是空洞的.我试过在viewDidLoad,viewWillAppear和viewDidAppear中做这个没有成功.对象self.moreNavigationController.topViewController的类型为UIMoreListController,它似乎没有文档,我在界面中看不到任何可以帮助我的东西.
有任何想法吗?
Ian*_*971 26
继Stephan的建议替换moreNavigationController的dataSource之后,这里是我实现的代码的快速视图.
我创建了一个名为MoreTableViewDataSource的新类,它实现了UITableViewDataSource协议.更多页面实际用于构建表的控制器称为UIMoreListControllerModern,它实现了UITableViewDataSource协议的必需部分.我的实现看起来像这样.
-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource
{
self = [super init];
if (self)
{
self.originalDataSource = dataSource;
}
return self;
}
- (void)dealloc
{
self.originalDataSource = nil;
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [originalDataSource tableView:table numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
cell.textColor = [UIColor whiteColor];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的CustomTabBarController类中,我重写viewDidLoad,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *moreController = self.moreNavigationController;
moreController.navigationBar.barStyle = UIBarStyleBlackOpaque;
if ([moreController.topViewController.view isKindOfClass:[UITableView class]])
{
UITableView *view = (UITableView *)moreController.topViewController.view;
view.backgroundColor = [UIColor blackColor];
moreTableViewDataSource = [[MoreTableViewDataSource alloc] initWithDataSource:view.dataSource];
view.dataSource = moreTableViewDataSource;
}
}
Run Code Online (Sandbox Code Playgroud)
这里要求的是头文件
@interface MoreTableViewDataSource : NSObject <UITableViewDataSource>
{
id<UITableViewDataSource> originalDataSource;
}
@property (retain) id<UITableViewDataSource> originalDataSource;
-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource;
@end
Run Code Online (Sandbox Code Playgroud)
和
#import "MoreTableViewDataSource.h"
@interface CustomTabBarController : UITabBarController
{
MoreTableViewDataSource *moreTableViewDataSource;
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*lot 21
只有在显示moreNavigationController后才会填充visibleCells.
并且单元格是在运行时创建的,因此即使您更改单元格的内容,它们也会在显示时被替换.
要尝试的一件事是替换moreNavigationController tableView的数据源,调用原始数据源的cellForRowAtIndexPath并在返回之前更改其内容.
使用下面的代码,在显示一次moreNavigationController来初始化之后,你会看到当你返回到moreNavigationController时,单元格是红色的,但是立即返回到白色背景.
UITableView *view = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
if ([[view subviews] count]) {
for (UITableViewCell *cell in [view visibleCells]) {
cell.backgroundColor = [UIColor redColor];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
感谢未知。按照他的解决方案,我将把他的代码放在 Swift 中。您唯一应该做的就是创建 MoreTableViewCell 类并创建它。您不必使用故事板。如果你想修改tableView,你可以在customizeMoreTableView方法中完成。
class TabBarMenuController: UITabBarController, UITableViewDelegate, UITableViewDataSource{
var tabBarItems: [UIViewController] = []
var areMessagesVisible: Bool = false
var titleForTabBars: [String] = ["resources", "events", "training", "my profile", "news", "contacts"]
var iconNames: [String] = ["film", "calendar", "classroom", "profile", "news", "Phone"]
var controllersStoryboardId: [String] = ["resourcesNavController", "eventsNavController", "enablementNavController", "profileNavController", "newsNavController", "contactsNavController"]
// to manage moreTableView
var moreTableView: UITableView = UITableView()
var currentTableViewDelegate: UITableViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.customizeMoreTableView()
//to REMOVE
areMessagesVisible = true
if !areMessagesVisible{
self.titleForTabBars.removeAtIndex(4)
self.controllersStoryboardId.removeAtIndex(4)
self.iconNames.removeAtIndex(4)
}
for i in 0 ..< controllersStoryboardId.count{
tabBarItems.append(UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier(controllersStoryboardId[i]) as? UINavigationController ?? UINavigationController())
}
self.moreNavigationController.navigationBar.tintColor = UIColor.blackColor()
}
override func viewWillAppear(animated: Bool) {
for i in 0 ..< tabBarItems.count{
tabBarItems[i].tabBarItem = UITabBarItem(title: titleForTabBars[i], image: UIImage(named: iconNames[i]), selectedImage: UIImage(named: iconNames[i]))
}
self.viewControllers = tabBarItems
}
func customizeMoreTableView(){
moreTableView = self.moreNavigationController.topViewController!.view as? UITableView ?? UITableView()
currentTableViewDelegate = moreTableView.delegate;
moreTableView.delegate = self
moreTableView.dataSource = self;
moreTableView.registerClass(MoreTableViewCell.self, forCellReuseIdentifier: "MoreTableViewCell")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let moreCell = tableView.dequeueReusableCellWithIdentifier("MoreTableViewCell", forIndexPath: indexPath) as? MoreTableViewCell ?? MoreTableViewCell()
moreCell.textLabel?.text = titleForTabBars[indexPath.row + 4]
moreCell.imageView?.image = UIImage(named: iconNames[indexPath.row + 4])
/*let testLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
testLabel.backgroundColor = UIColor.yellowColor()
moreCell.addSubview(testLabel)
*/
return moreCell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleForTabBars.count - 4
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
currentTableViewDelegate?.tableView!(tableView, didSelectRowAtIndexPath: indexPath)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23616 次 |
| 最近记录: |